Thursday, January 20, 2011

18.Arrays


Array definition

Array by definition is a variable that hold multiple elements which has the same data type.

Declaring Arrays

We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name. Here is the syntax:
1data_type array_name[size];
For example, to declare an integer array which contains 100 elements we can do as follows:
1int a[100];
There are some rules on array declaration. The data type can be any valid C data types including structure and union. The array name has to follow the rule of variable and the size of array has to be a positive constant integer.
We can access array elements via indexes array_name[index]. Indexes of array starts from 0 not 1 so the highest elements of an array is array_name[size-1].

Initializing Arrays

It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name. Here is an example of initializing an integer array.
1int list[5] = {2,1,3,7,8};

Array and Pointer

Each array element occupies consecutive memory locations and array name is a pointer that points to the first element. Beside accessing array via index we can use pointer to manipulate array. This program helps you visualize the memory address each array elements and how to access array element using pointer.
01#include <stdio.h>
02  
03void main()
04{
05     
06    const int size = 5;
07     
08    int list[size] = {2,1,3,7,8};
09     
10    int* plist = list;
11  
12    // print memory address of array elements
13    for(int i = 0; i < size;i++)
14    {
15        printf("list[%d] is in %d\n",i,&list[i]);
16     
17    }
18  
19    // accessing array elements using pointer
20    for(i = 0; i < size;i++)
21    {
22        printf("list[%d] = %d\n",i,*plist);
23         
24        /* increase memory address of pointer so it go to the next
25           element of the array */
26        plist++;
27    }
28  
29}
Here is the output
list[0] is in 1310568
list[1] is in 1310572
list[2] is in 1310576
list[3] is in 1310580
list[4] is in 1310584
list[0] = 2
list[1] = 1
list[2] = 3
list[3] = 7
list[4] = 8
You can store pointers in an array and in this case we have an array of pointers. This code snippet use an array to store integer pointer.
1int *ap[10];

Multidimensional Arrays

An array with more than one index value is called a multidimensional array. All the array above is called single-dimensional array. To declare a multidimensional array you can do follow syntax
1data_type array_name[][][];
The number of square brackets specifies the dimension of the array. For example to declare two dimensions integer array we can do as follows:
1int matrix[3][3];
Initializing Multidimensional Arrays
You can initialize an array as a single-dimension array. Here is an example of initialize an two dimensions integer array:
1int matrix[3][3] =
2{
3  {11,12,13},
4  {21,22,23},
5  {32,31,33},
6};

Dont Miss Another Post Connect With Us !

Enter your email address:

0 comments: