Loading, please wait...

Pointer-Array Relationship

Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used to do any operation involving array subscripting.

 

Assume that integer array b[5] and integer pointer variable bPtr have been defined. Since the array name (without a subscript) is a pointer to the first element of the array, we can set bPtr equal to the address of the first element in array b with the statement

bPtr = b;

 

This statement is equivalent to taking the address of the array’s first element as follows:

bPtr = &b[ 0 ];

 

Array element b[3] can alternatively be referenced with the pointer expression

*( bPtr + 3 )

The 3 in the above expression is the offset to the pointer. When the pointer points to the beginning of an array, the offset indicates which element of the array should be referenced, and the offset value is identical to the array subscript.

 

The preceding notation is referred to as pointer/offset notation. The parentheses are necessary because the precedence of * is higher than the precedence of +. Without the parentheses, the above expression would add 3 to the value of the expression *bPtr (i.e., 3 would be added to b[0], assuming bPtr points to the beginning of the array).

 

Just as the array element can be referenced with a pointer expression, the address

&b[ 3 ]

can be written with the pointer expression

bPtr + 3

 

The array itself can be treated as a pointer and used in pointer arithmetic. For example, the expression

*( b + 3 )

also refers to the array element b[3]. In general, all subscripted array expressions can be written with a pointer and an offset. In this case, pointer/offset notation was used with the name of the array as a pointer. The preceding statement does not modify the array name in any way; b still points to the first element in the array. Pointers can be subscripted exactly as arrays can.

 

For example, if bPtr has the value b, the expression

bPtr[ 1 ]

refers to the array element b[1]. This is referred to as pointer/subscript notation. Remember that an array name is essentially a constant pointer; it always points to the beginning of the array. Thus, the expression

b += 3

is invalid because it attempts to modify the value of the array name with pointer arithmetic.

 

Attempting to modify an array name with pointer arithmetic is a syntax error.The example uses the four methods we have discussed for referring to array elements—array subscripting, pointer/offset with the array name as a pointer, pointer subscripting, and pointer/offset with a pointer—to print the four elements of the integer array b.

 

Example:

/*Using subscription and pointer notations with arrays */

#include <stdio.h>

int main( void )
{
int b[] = { 10, 20, 30, 40 }; /* initialize array b */
int *bPtr = b; /* set bPtr to point to array b */
int i; /* counter */
int offset; /* counter */
/* output array b using array subscript notation */
printf( "Array b printed with:\nArray subscript notation\n" );

/* loop through array b */
for ( i = 0; i < 4; i++ ) {
printf( "b[ %d ] = %d\n", i, b[ I  ]  );
} /* end for */
/* output array b using array name and pointer/offset notation */
printf( "\nPointer/offset notation where\n"
"the pointer is the array name\n" );

/* loop through array b */
for ( offset = 0; offset < 4; offset++ ) {
printf( "*( b + %d ) = %d\n", offset, *( b + offset ) );
} /* end for */
/* output array b using bPtr and array subscript notation */
printf( "\nPointer subscript notation\n" );

/* loop through array b */
for ( i = 0; i < 4; i++ ) {
printf( "bPtr[ %d ] = %d\n", I, bptr[ I  ] );
} /* end for */

/* output array b using bPtr and pointer/offset notation */
printf( "\nPointer/offset notation\n" );

/* loop through array b */
for ( offset = 0; offset < 4; offset++ ) {
printf( "*( bPtr + %d ) = %d\n", offset,*( bPtr + offset ) );
} /* end for */

return 0; /* indicates successful termination */
} /* end main */

 

Output:

Array b printed with:

Array subscript notation

b[ 0 ] = 10

b[ 1 ] = 20

b[ 2 ] = 30

b[ 3 ] = 40

Pointer/offset notation where

the pointer is the array name

*( b + 0 ) = 10

*( b + 1 ) = 20

*( b + 2 ) = 30

*( b + 3 ) = 40

Pointer subscript notation

bPtr[ 0 ] = 10

bPtr[ 1 ] = 20

bPtr[ 2 ] = 30

bPtr[ 3 ] = 40

Pointer/offset notation

*( bPtr + 0 ) = 10

*( bPtr + 1 ) = 20

*( bPtr + 2 ) = 30

*( bPtr + 3 ) = 40