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 The function printarr prints the value of each element in arr.
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.
Each array element occupies consecutive memory locations.
You can print addresses using place holders %16lu or %p.
Point to Remember For array elements, consecutive memory locations are allocated
|
No responses found. Be the first to respond and make money from revenue sharing program.
|