C program to print elements of an array and address of each element

C program to print array elements and address of each element :

In this tutorial, we will learn how to print the address and elements of an integer array. This example will show you how elements of an array are stored in memory . Let’s take a look at the program :

C program :

#include <stdio.h>

int main()
    int myArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int i;

    for (i = 0; i < 9; i++)
        printf("Address for %d is %d\n", myArray[i], &myArray[i]);

Explanation :

The commented numbers in the above program denote the step number below :

  1. Create one integer array myArray with some integer values. We will print these numbers and memory address where it is stored. Integer i is used in the loop below.
  2. Run one for loop to read all numbers from the array. Print the number and its address. For printing the address we are using &myArray[i] for position i.

Output :

Address for 1 is 1489689280
Address for 2 is 1489689284
Address for 3 is 1489689288
Address for 4 is 1489689292
Address for 5 is 1489689296
Address for 6 is 1489689300
Address for 7 is 1489689304
Address for 8 is 1489689308
Address for 9 is 1489689312

The size of a integer is 4 bytes. That’s why you can see difference of 4 between each address. Also all numbers in the array are stored in consecutive contiguous memory locations.

You might also like: