C - Memory Locations in an Array


Individual array elements have memory locations.

The & operator prefixes the specific element variable returns its address.

The %p conversion character in the printf() function displays the address.


#include <stdio.h> 

int main() //from   ww w .  ja  va2  s .  c om
   char hello[] = "Hello!"; 
   int i = 0; 

       printf("%c at %p\n",hello[i],&hello[i]); 


The addresses of array are contiguous in memory, one byte after another.

Variables in C have a name, type, value, and location.

The variable's type is closely tied to the variable's size in memory, which is obtained by using the sizeof operator.

A variable's value is set or used directly in the code.

The variable's location is shown courtesy of the & operator and the %p conversion character.

Related Topic