C - Memory Locations in an Array

Introduction

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.

Demo

#include <stdio.h> 

int main() //ww  w.  j  av a  2s  .  c o  m
{ 
   char hello[] = "Hello!"; 
   int i = 0; 

   while(hello[i]) 
   { 
       printf("%c at %p\n",hello[i],&hello[i]); 
       i++; 
   } 
   return(0); 
}

Result

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