Industrial Training




Pointers 2



Consider the declaration,

int i = 3 ;

This declaration tells the C compiler to:

  1. Reserve space in memory to hold the integer value.

  2. Associate the name i with this memory location.

  3. Store the value 3 at this location.

We may represent i 's location in the memory by the following memory map:


Figure 1

We see that the computer has selected memory location 6485 as the place to store the value 3. This location number 6485 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 3.

We can print this address through the following statement :
printf ( "\nAddress of i = %u", &i ) ; /* o/p : 6485 */

Look at the printf( ) statement carefully. '&' used in this statement is C's 'address of' operator. The expression &i returns the address of the variable i, which in this case happens to be 6485.

The other pointer operator available in C is '*', called 'value at address' operator. It returns the value stored at a particular address. The 'value at address' operator is also called 'indirection' operator.

Observe carefully the following statements :

printf ( "\nAddress of i = %u", &i ) ; /* o/p : 6485 */

printf ( "\nValue of i = %d", i ) ; /* o/p : 3 */

printf ( "\nValue of i = %d", *( &i ) ) ; /* o/p : 3 */

Note that printing the value of *( &i ) is same as printing the value of i.

Let us now see what are pointers and how they can be used in various expressions. We have seen in the above section that the expression &i returns the address of i. If we so desire this address can be collected in a variable by saying,

j = &i ;

But remember that j is not an ordinary variable like any other integer variable. It is a variable which contains the address of another variable ( i in this case ).

Since j is a variable, the compiler must provide it space in memory. The following memory map illustrates the contents of i and j.


Figure 2

As you can see, i 's value is 3 and j 's value is i 's address.

But, we can't use j in a program without declaring it. And since j is a variable which contains the address of i, it is declared as,

int *j ;

This declaration tells the compiler that j will be used to store the address of an integer value - in other words j points to an integer.

How do we justify the usage of * in the declaration,

int *j ;

Let us go by the meaning of *. It stands for 'value at address'. Thus, int *j would mean:

  1. value at address stored in j is an int

  2. j contains address of an int

  3. j points to an int

  4. j is a pointer which points in the direction of an int

So we can conclude that pointer is a variable which contains address of another variable.

More pointer types
The way we have used an int pointer in the above example we can also build a char pointer, a float pointer, a long int pointer, etc. This is shown below:

char ch = 'A' ;

float a = 3.14 ;

long int j = 40000L ;

char *dh ;

float *b ;

long int *k ;

dh = &ch ;

b = &a ;

k = &j ;

Pointer to Pointer
If we store address of an integer pointer in a variable that variable becomes pointer to an integer pointer. And if we store address of a pointer to an integer pointer in a variable that becomes a pointer to a pointer to an integer pointer. This is shown in the following code segment:

int i = 10 ;

int *j ; // integer pointer

int **k ; // pointer to an integer pointer

int ***l ; // pointer to a pointer to an integer pointer

j = &i ;

k = &j ;

l = &k ;

// print 10 using i, j, k, l

printf ( "%d %d %d %d", i, *j, **k, ***l ) ;

Pointer Arithmetic
Following operations can be performed on a pointer:

  1. Addition of a number to a pointer. For example,

int i = 4, *j, *k ;

 

j = &i ;

j = j + 1 ;

j = j + 9 ;

k = j + 3 ;

  1. Subtraction of a number from a pointer. For example,

int i = 4, *j, *k ;

j = &i ;

j = j - 2 ;

j = j - 5 ;

k = j - 6 ;

  1. Subtraction of a pointer from a pointer. For example,

int i = 4, j = 5, *p, *q, d ;

p = &i ;

q = &j ;

d = q - p ;

The following program catches the essence of all that we have said about pointers so far.

main( )

{

float a = 3.14, *b ;

char ch = 'z', *dh ;

int i = 25, *j ;



Hi I am Pluto.