C Program to Read an Array and Display Its Value & Address Using Pointer

Question: write a program in C to read an array containing n elements and display value and address of each element using pointer.

C Source Code: Read Array & Display Value, Address Using Pointer


#include<stdio.h>

int main()
{
    int arr[100], *ptr, i, n;

    printf("How many numbers to read? (< 100): ");
    scanf("%d", &n);
    /* Pointer referencing */
    ptr = &arr[0]; /* same as ptr = arr; */

    /* Reading array using pointer */
    printf("Enter %d numbers:\n", n);
    for(i=0;i< n;i++)
    {
        printf("arr[%d] = ", i);
        scanf("%d", (ptr+i));
    }

    /* Displaying array content */
    printf("\nArray content read from user is: \n");
    for(i=0;i< n;i++)
    {
        printf(" Value = %d\t Address = %u\n", *(ptr+i), (ptr+i));
    }

    return 0;
}

Output

The output of the above program is:

How many numbers to read? (< 100): 6
Enter 6 numbers:
arr[0] = 10
arr[1] = -20
arr[2] = 30
arr[3] = -40
arr[4] = 50
arr[5] = -60

Array content read from user is:
 Value = 10      Address = 6421632
 Value = -20     Address = 6421636
 Value = 30      Address = 6421640
 Value = -40     Address = 6421644
 Value = 50      Address = 6421648
 Value = -60     Address = 6421652