Friday, March 18, 2011

Program for Address Of Each Element In arrays with Explanation


Program

#include <stdio.h>
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.

No comments:

Post a Comment