Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Data Types : Part 1 Page - 4

Page 4 of 4
Author: Exforsys Inc.     Published on: 7th Mar 2006    |   Last Updated on: 21st Aug 2011

C Programming - Data Types : Part 1

Derived types

Derived types are types constructed from the ones we've seen so far in this tutorial. There are three main derived types: arrays, structures and unions.

Arrays

You have probably seen arrays in other programming languages. Arrays are one or more instances of one type. You declare arrays with a type name followed by variable name and then the size of the array in brackets:

Sample Code
  1. char name[50];
  2. long long num_array[256];
  3. float m[2][4];
Copyright exforsys.com


Ads

The first two lines declare name as an array of 50 characters and num_array as an array of 256 long longs. The third line declares m as a 2-dimensional array of floats, like a matrix with 2 rows and 4 columns.

In C you cannot assign to arrays except when they are first declared or if they are members of a struct. So copying an array like this:

Sample Code
  1. int arr1[4] = { 1, 2, 3, 4 };
  2. int arr2[4];
  3.  
  4. arr2 = arr1;
Copyright exforsys.com


will not work. There are ways around this, like using a for loop to copy each element of the array, or using memcpy().

Array elements are accessed using the [] operator, and elements are numbered starting at 0. It is also possible to declare variable sized arrays (if they meet certain conditions). Let's see some examples of using arrays:

Sample Code
  1. #include <stdio.h>
  2.    #include <stdlib.h>
  3.    
  4.    void main()
  5.    {
  6.            int numarr[ 4 ] = { 12, 92, 54, 890 };
  7.            int sz;
  8.            char buff[64];
  9.            float m[2][4] = { { 1.1, 1.2, 1.3, 1.4 }, { 2.1, 2.2, 2.3, 2.4 }};
  10.  
  11.           printf( "numarr's 3rd element is %d\n", numarr[2] );
  12.           printf( "m's 2nd row's 4th column value is %f\n", m[1][3] );
  13.  
  14.           printf("Enter size of variable sized array: ");
  15.           fgets( buff, 64, stdin );
  16.           sz = atoi(buff);
  17.  
  18.           int arr2[ sz ];
  19.           int i;
  20.           for( i = 0; i < sz; ++i )
  21.                   arr2[ i ] = i+1;
  22.  
  23.           for( i = 0; i < sz; ++i )
  24.                   printf("arr2[%d] = %d\n", i, arr2[ i ]);
  25.   }
Copyright exforsys.com


The program's output follows:

numarr's 3rd element is 54 m's 2nd row's 4th column value is 2.400000 Enter size of variable sized array: 5 arr2[0] = 1 arr2[1] = 2 arr2[2] = 3 arr2[3] = 4 arr2[4] = 5

On lines 6 and 9 are examples of initializing an array when you declare it. The initial values are inside the { } symbols separated by commas. On line 9, each of the two rows of m is initialized in two separate { } initializers. Lines 11-12 show that to access element N of an array you use index N-1 since the first element has index 0. The third element of numarr is accessed with index [2] and the second row and fourth column of m is accessed with index [1][3].

Ads

Line 18 declares a variable sized array. All the other arrays in this program are constant size - their sizes never change and can be calculated at compile time. The arr2 array's size depends on the number the user enters when the program is run. On this run the user enters 5 so it is 5 elements long. Lines 20-21 initialize the array so that the element at index 0 gets the number 1, element at 1 gets the number 2, and so on. Lines 23-24 print out the elements.

Arrays will be discussed further in another tutorial.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments