Arrays and Pointers

An array's identifier (name or label) is actually a pointer to the first element of an array. That is myArray === &myArray[0]  (or even &myArray ). The only difference is that the array name is a constant pointer (cannot change the location it points at).

 

int myArray[5] = {1, 2, 3, 4, 5} ;

 

The address of the first element is: &myArray[0]

 

Therefore &myArray[0] is the same as myArray, as are their addresses

 

The value in &myArray[0] is myArray[0], and the value in myArray is *myArray, and therefore myArray[0] is the same as *myArray

 

&myArray[1] === (myArray+1)  AND, myArray[1] === *(myArray+1)
&myArray[2] === (myArray+2)  AND, myArray[2] === *(myArray+2)
&myArray[3] === (myArray+1)  AND, myArray[3] === *(myArray+3)
.
.
&myArray[i] === (myArray+i)  AND, myArray[i] === *(myArray+i)

 

#include <stdio.h>

int main ()
{
	int myArray[7] = {98, 82, 73, 57, 65, 76, 44};

	int i = 0 ;

	for( i = 0; i < 7 ; i++ )
	{

		printf("The address of myArray element %d is: %d \n", i, &myArray[i] );
		printf("The address of myArray element %d is: %d \n", i, (myArray + i) );

		printf("The value in myArray element %d is: %d \n", i, myArray[i] );
		printf("The value in myArray element %d is: %d \n\n", i, *(myArray + i) );

	}

	return 0;
}

Leave a Reply