Input and print array elements using a pointer with C.
input and print array elements using pointer in array notation
#include <stdio.h>
int main()
{
int arr[100];
int N, i;
int *ptr = arr; // Pointer to arr[0]
printf("Enter size of array: ");
scanf("%d", &N);
printf("Enter elements in array:\n");
for (i = 0; i < N; i++)
{
// &ptr[i] is equivalent to &arr[i]
scanf("%d", &ptr[i]);
}
printf("Array elements: ");
for (i = 0; i < N; i++)
{
printf("%d, ", i[ptr]);
}
return 0;
}
Output
Enter size of array : 5
Enter elements in array : 1 2 3 4 5
Array elements : 1 2 3 4 5
0 Comments