blogger Indonesia

Follow judhyns blog

ADDRESS OF EACH ELEMENT IN AN ARRAY

Introduction

Each element of the array has a memory address. The following program prints an array limit value and an array element address.

Program

 #include  void printarr(int a[]); main() {     int a[5];     for(int i = 0;i<5;i++)     {         a[i]=i;     }     printarr(a); } void printarr(int a[]) {     for(int i = 0;i<5;i++)     {         printf("value in array %d\n",a[i]);     } } void printdetail(int a[]) {     for(int i = 0;i<5;i++)     {         printf("value in array %d and address is %16lu\n",a[i],&a[i]); \\ A     } } 

Explanation

  1. The function printarr prints the value of each element in arr.

  2. The function printdetail prints the value and address of each element as given in statement A. Since each element is of the integer type, the difference between addresses is 2.

  3. Each array element occupies consecutive memory locations.

  4. You can print addresses using place holders %16lu or %p.

Point to Remember

For array elements, consecutive memory locations are allocated.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |