C - Array Array Addresses


Here's a declaration for an array with four elements:

long number[4];

The array name, number, identifies the address in memory where the array elements are stored.

The location of an element is specified by combining the array name with the index value.

The index value represents the offset of a number of elements from the beginning of the array.

You can get the address of an array element in the same way as for an ordinary variable.

For a variable with the name value, you would use the following statement to print its address:

printf("\n%p", &value);

To output the address of the third element of an array with the name number:

printf("\n%p", &number[2]);

The following fragment sets a value for each element in an array and outputs the address and contents of each element:
int data[5];
for(unsigned int i = 0 ; i < 5 ; ++i)
  data[i] = 12*(i + 1);
  printf("data[%d] Address: %p  Contents: %d\n", i, &data[i], data[i]);

The for loop variable i iterates over all the legal index values for the data array.