C Program to Read an Array and Display Its Value & Address Using Array Itself as a Pointer
Question: write a program in C to read an array containing n elements and display value and address of each element using array name itself as a pointer.
C Source Code: Read & Display Array Using Array Itself Pointer
When using array name itself as a pointer, note that array name always holds the address of first element in the array. In the following program, array name arr
is equivalent to &arr[0]
. So array name is considered as indirect pointer.
#include<stdio.h>
int main()
{
int arr[100], i, n;
printf("How many numbers to read? (< 100): ");
scanf("%d", &n);
/* Reading array using array itself as a pointer */
printf("Enter %d numbers:\n", n);
for(i=0;i< n;i++)
{
printf("arr[%d] = ", i);
scanf("%d", (arr+i));
}
/* Displaying array content */
printf("Array content read from user is: \n");
for(i=0;i< n;i++)
{
printf(" Value = %d\t Address = %u\n", *(arr+i), (arr+i));
}
return 0;
}
Output
The output of the above program is:
How many numbers to read? (< 100): 5 Enter 5 numbers: arr[0] = 11 arr[1] = 22 arr[2] = 33 arr[3] = 44 arr[4] = 55 Array content read from user is: Value = 11 Address = 6421632 Value = 22 Address = 6421636 Value = 33 Address = 6421640 Value = 44 Address = 6421644 Value = 55 Address = 6421648