Industrial Training




Pointers and arrays



An array is a collection of similar elements stored in adjacent memory locations.

int a[] = { 10, 13, -24, -35, 67 }

float b[] = { 1.2, 3.44, -5.44, 6.7, 8.9 } ;

long int c[25] ;

If an array is defined and initialized at the same place mentioning its dimension is optional.

If similarity and adjacency considerations are met we can build an array of anything, like say, an array of doubles, an array

of structures, an array of pointers etc.

Size of an array is sum of sizes of individual elements of an array.

Base address of an array if address of zeroth element of the array.

Mentioning the name of the array fetches its base address.

int a[] = { 10, 13, -24, -35, 67 } ;

printf ( "%u %u", a, &a[0] ) ; // both would give base address

Array can be passed to a function element by element. Alternatively we can pass the entire array to a function at one shot.

int a[] = { 10, 13, -24, -35, 67 } ;

int i ;

// passing the array element by element

for ( i = 0 ; i < 5 ; i++ )

display ( a[i] ) ;

// passing entire array

show ( a, sizeof ( a ) / 2 ) ;

To pass an entire array we simply need to pass its base address. Whenever we pass an entire array to the function we also

need to pass the size of the array, since the function has no way to find out how many elements are present in the array.

Array elements can be accessed using the subscript notation or the pointer notation.

int a[] = { 10, 13, -24, -35, 67 } ;

int i ;

// access using subscript notation

for ( i = 0 ; i < 5 ; i++ )

printf ( "%d", a[i] ) ;

// accessing using pointer notation

for ( i = 0 ; i < 5 ; i++ )

printf ( "%d", * ( a + i ) ) ;

Subscript notation is converted by the compiler into the pointer notation. Hence pointer notation would work faster since

using it we would be able to save on the conversion time.

All four following expression are same:

a[i]

( a + i )

( * i + a )

i[a]

In the expression a[i]

Out of a and i one must be an integer. The other may either be an array name or a pointer.

An array of pointers is different than a pointer to an array.

int *a[10] ;

int ( *b )[10] ;

a is an array of pointers, whereas, b is pointer to an array. Incrementing a using a++ is illegal. On incrementing b it would

start pointing to the next location of its type.

What would be the output of following program

main( )

{

int arr[ ] = { 0, 1, 2, 3, 4 } ;

int *ptr ;

for ( ptr = arr + 4 ; ptr >>= arr ; ptr-- )

printf ( "%d ", arr [ ptr - arr ] ) ;

}

Output


4 3 2 1 0

Explanation
A picture is worth a thousand words. Going by this dictum, the following figure 1 should add clarity to your understanding of the program.


Now things are getting really complicated, as the printf( ) would justify. Let us begin with the for loop. Firstly ptr is assigned the address 6012, the address of the fourth integer from the base address. Since this address is greater than the base address, the condition is satisfied and the control reaches printf( ) . What does arr [ ptr - arr ] evaluate to? ptr - arr means 6012 - 6004 , which yields 4, and hence arr[4] prints out the fourth element of the array. Then ptr-- reduces ptr to 6010. Since 6010 is greater than the base address 6004, the condition is satisfied and once again the control reaches the printf( ) . This time ptr - arr becomes 6010 - 6004 , i.e. 3. Thus arr[3] prints out 3. This process is repeated till all the integers in the array have been printed out. Possibly an easier way of understanding the expression ptr - arr would be as follows. Suppose ptr contains 6012 and arr contains 6004. We can then view the subtraction as ( arr + 4 - arr ) , since ptr is nothing but arr + 4 . Now I suppose its quite logical to expect the result of the subtraction as 4.

What would be the output of following program

main( )

{

static int a[ ] = { 0, 1, 2, 3, 4 } ;

static int *p[ ] = { a, a + 1, a + 2, a + 3, a + 4 }

int **ptr = p

printf ( "\n%u %d", a, *a )

printf ( "%u %u %d", p, *p, **p )

printf ( "\n%u %u %d", ptr, *ptr, **ptr ) ;

}

Output
6004 0
9016 6004 0
9016 6004 0

Explanation


Look at the initialisation of the p[ ] . During initialisation, the addresses of various elements of the array a[ ] are stored in

the array p[ ] . Since p[ ] contains addresses of integers, it has been declared as an array of pointers to integers. Figure 2

shows the content a[ ] and p[ ] . In the variable ptr , the base address of the array p[ ] , i.e. 9016 is stored. Since this

address is the address of p[0] , which itself is a pointer, ptr has been declared as pointer to an integer pointer. Let us

understand the printf( ) now. The first printf( ) is quite simple. printf ( "\n%u %d", a, *a ) ; It prints out the base address of

the array a[ ] and the value at this base address.



Hi I am Pluto.