Write a C program to input elements in an array and print array using pointers. How to input and display array elements using pointer in C programming.
Example
Input
Input array size: 10 Input elements: 1 2 3 4 5 6 7 8 9 10
Output
Array elements: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Required knowledge
Basic C programming, Array, Pointers, Pointers and Array
Learn to input and print array without pointer.
How to access array using pointer
Array elements in memory are stored sequentially. For example, consider the given array and its memory representation

If you have a pointer say ptr
pointing at arr[0]
. Then you can easily apply pointer arithmetic to get reference of next array element. You can either use (ptr + 1)
or ptr++
to point to arr[1]
.
Program to input and print array elements using pointer
But wait before moving on to next program, there is another way to write the above program. I must say the better way to deal with arrays using pointer is, instead of incrementing pointer use pointer addition.
Program to input and print array using pointers – best approach
Note:
(ptr + i)
is equivalent to&ptr[i]
, similarly*(ptr + i)
is equivalent toptr[i]
. Also you can use(i + ptr)
,i[ptr]
all means the same. Read more about array indexes and pointer.
So let us finally write the same program using array notation, for those who prefer array notation more over pointer notation.
Program to input and print array using pointer in array notation
Before you move on make sure you have learned to read and print array using recursion.
Output
Enter size of array: 10 Enter elements in array: 1 2 3 4 5 6 7 8 9 10 Array elements: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,