Pointer to an Array

An array name is a constant pointer to the first element of the array.

 

(constant pointer cannot change the location it points at)

 

int *myPointer ;

int myArray[42] ;

 

myPointer = myArray ;

 

myArray is a pointer to &myArray[0] which is the first element of myArray

 

#include <stdio.h>
int main ()
{
	int *myPointer ;
	int myArray[7]= {98, 82, 73, 57, 65, 76, 44};

	myPointer = myArray ;

	printf("myPointer: %x contains the address of myArray: %x \n", myPointer, &myArray);

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

		printf( "Pointer to element %d of myArray contains %d \n", i, *(myPointer + i) );
		printf( "Directly accessing element %d of myArray: %d \n\n", i, myArray[i] );
	}

	return 0;
}

Note: line 13 gives a brief intro to pointer arithmetic...

Leave a Reply