Showing posts with label Array and pointer. Show all posts
Showing posts with label Array and pointer. Show all posts

Friday 30 March 2012

Array and pointer


Array and pointer

One dimensional array
When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of array. Array store elements in contiguous memory locations.
For ex,
int x[5];


Element
X[0]
X[1]
X[2]
X[3]
X[4]


Value

1
2
3
4
5

Address
 
100

102

104

05

106

                      Base Address                                


Create pointer variable
int *p;      // declare poiner
p=&x[0];  // assign base address
   or
p=x;

now the value of x can be accessed by pointer as follows:
p=&x[0] (100)
p+1=&x[1] (102)
p+2=&x[2] (104)
p+3=&x[3] (106)
p+4=&x[4] (108)

Here, the address of an element is calculated using its index and scale factor of data type

For ex,
Address of x[3]=base address+(3*scale factor of int)

100 + (3*2) = 106

When handling arrays, instead of using array indexing , pointers can be used to access array elements.

For ex, *(p+3) is equivalent to x[3]

The pointer accessing method is much faster than array indexing.
Ex,
  void main()
   {
      int a[] = {1,2,3,4,5};
      int *p,i;
      p=a;
      for(i=0;i<5;i++)
       {
          printf(“\n number %d = %d”,p+i);
       }
   }   


Two dimensional array

Two dimensional and Multidimensional arrays also represented using pointer.
Two dimensional array declared as follows:
          int p[3][3] = {{1,2,3},{4,5,6},{7,8,9}};



Column[0]
Column [1]
Column [2]
Row[0]
1
[0][0]
2
[0][1]
3
[0][2]
Row [1]
4
[1][0]
5
[1][1]
6
[1][2]
Row [2]
7
[2][0]
8
[2][1]
9
[2][2]
















Here,
          p                 = pointer to first row
          p+1             = pointer to first row
          p+i              = pointer to ith row
          *(p+i)                   = pointer to first element in ith row
          *(p+i)+j      = pointer to jth element in ith row
          *(*(p+i)+j) = value stored in the cell(i,j)
 ith row and jth column
Ex
          element p[2][2] represented as : *(*(p+2)+2)

The basic address of array is p[0][0] and starting this address compiler allocates contiguous space for all the elements row wise. That is , the first element of second row is placed immediately after the last element of first row. Element stored as:

1
2
3
4
5
6
7
8
9
row1
row2
row3

A two dimensional array is actually a collection of one dimensional arrays. Therefore two dimensional array defined as a pointer to a group of contiguous on dimensional arrays.

A two dimensional array can be declared as follows :

data-type (*ptr_var) [expression2]; 

Here, data-type refers to the data type of array , ptr_var is the name of the pointer variable , expression2 specifies the number of columns.

Ex
          int (*x)[20];  // similar as int x[10][20];

In this declaration x is defined to be pointer to a group of contiguous , one dimensional arrays. Thus x, points to the first 20-element array, which is actually the first row(row0) of the original two dimensional array. Similarly , (x+1) points to the second 20 elements array , which is the second row(row1) of the original two dimensional array , and so on.

Posted By : Ruchita Pandya