How to Get the Address of an Array in C Programming

By Dan Gookin

An array is a type of variable in C programming, one that you can examine for its size and address. Now you uncover the deep, dark secret of beholding an array’s address.

The source code from Where the Array Lurks shows a teensy program that declares an int array and then displays that array’s location in memory. Simple.

WHERE THE ARRAY LURKS

#include <stdio.h>
int main()
{
 int array[5] = { 2, 3, 5, 7, 11 };
 printf("'array' is at address %pn",&array);
 return(0);
}

Exercise 1: Type the source code from Where the Array Lurks into your editor. Build and run the program.

Here’s the output:

'array' is at address 0028FF0C

Exercise 2: Duplicate Line 7 in the code to create a new Line 8, removing the ampersand:

printf("'array' is at address %pn",array);

The main difference is the missing & that prefixes the array variable. Will it work? Compile and run to be sure.

Here’s the output for the new code:

'array' is at address 0028FF0C
'array' is at address 0028FF0C

Is the & prefix necessary? Better find out:

O VARIABLE, WHEREFORE ART THOU?

#include <stdio.h>
int main()
{
  char c = 'c';
  int i = 123;
  float f = 98.6;
  double d = 6.022E23;
  printf("Address of 'c' %pn",&c);
  printf("Address of 'i' %pn",&i);
  printf("Address of 'f' %pn",&f);
  printf("Address of 'd' %pn",&d);
  return(0);
}

Exercise 3: Summon the source code from O Variable, Wherefore Art Thou? Edit Lines 10 through 14 to remove the & from the variable’s name in the printf() statement. Attempt to build the program.

Here’s the error message:

Warning: format '%p' expects type 'void *' ...

Obviously, the & is important for individual variables. But for arrays, it’s optional and, indeed, ignored. But how could that be, unless . . . unless an array is really a pointer!