Array implementation of pointers
C program to input and print array elements using pointers.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int arr[MAX];
int n, i;
int *ptr;
ptr = arr; //pointer ptr points to arr[0]
printf("Enter the size of the array\t:");
scanf("%d",&n);
printf("\nEnter elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%d",ptr);
ptr++;
}
ptr = arr;
printf("\nElements in the array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",*ptr);
ptr++;
}
getch();
}
Output
Enter the size of the array :5
Enter elements in the array
1
3
5
7
9
Elements in the array are
1
3
5
7
9
Explanation
In the above program, an integer array 'arr' is declared and a pointer variable 'ptr' is also declared.
ptr = arr statement points the ptr to arr[0] element of the array.
To take input from the user 'ptr' is used.
Note that, & (address of) operator is not used in the scanf() function. scanf() takes the actual memory address where the user input is to be stored. The pointer variable 'ptr' contains the actual memory address of arr[0]. Therefore, there is no need to prefix the & operator in scanf() function in case of pointers.
Now, next input is to be taken in arr[1]. But, pointer ptr points to arr[0]. So, for this we increment ptr using the statement ptr++; Now, ptr points to arr[1]. and next element entered by user is stored in arr[1].
The whole process is repeated 'n' number of times.
At the end of above loop ptr will point to arr[n]. But, we have to traverse the array from beginning to print it. For this, pointer ptr will be again pointing to arr[0] using the statement ptr = arr;
Now, we have to print the array, for this *ptr is used.
Initially ptr points to arr[0]. So, *ptr will give the value stored in arr[0]. This value is printed and pointer is incremented (ptr++) to point to the next element of the array. The above process is repeated 'n' number of times.
There is another way to write the above program. Instead of incrementing pointer use pointer addition. This is shown in Method 2 tab.
ptr = arr statement points the ptr to arr[0] element of the array.
To take input from the user 'ptr' is used.
for(i=0;i<n;i++)
{
scanf("%d",ptr);
ptr++;
}
Note that, & (address of) operator is not used in the scanf() function. scanf() takes the actual memory address where the user input is to be stored. The pointer variable 'ptr' contains the actual memory address of arr[0]. Therefore, there is no need to prefix the & operator in scanf() function in case of pointers.
Now, next input is to be taken in arr[1]. But, pointer ptr points to arr[0]. So, for this we increment ptr using the statement ptr++; Now, ptr points to arr[1]. and next element entered by user is stored in arr[1].
The whole process is repeated 'n' number of times.
At the end of above loop ptr will point to arr[n]. But, we have to traverse the array from beginning to print it. For this, pointer ptr will be again pointing to arr[0] using the statement ptr = arr;
Now, we have to print the array, for this *ptr is used.
for(i=0;i<n;i++)
{
printf("%d\n",*ptr);
ptr++;
}
Initially ptr points to arr[0]. So, *ptr will give the value stored in arr[0]. This value is printed and pointer is incremented (ptr++) to point to the next element of the array. The above process is repeated 'n' number of times.
There is another way to write the above program. Instead of incrementing pointer use pointer addition. This is shown in Method 2 tab.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int arr[MAX];
int n, i;
int *ptr;
ptr = arr; //pointer ptr points to arr[0]
printf("Enter the size of the array\t:");
scanf("%d",&n);
printf("\nEnter elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%d",(ptr+i));
}
printf("\nElements in the array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(ptr+i));
}
getch();
}
Output
Enter the size of the array :5
Enter elements in the array
1
3
5
7
9
Elements in the array are
1
3
5
7
9
Explanation
In this program, (ptr+i) is used instead of ptr.
(ptr+i) is equivalent to &arr[i].
This means,
(ptr+0) => &arr[0]
(ptr+1) => &arr[1]
(ptr+2) => &arr[2]
and so on..
Note that, we are not incrementing pointer. So, ptr still points to arr[0] only. So, there is no need to reassign ptr to arr as done in the previous program.
Similarly, to print the values, *(ptr+i) is used instead of *ptr
*(ptr+i) is equivalent to arr[i].
This means,
*(ptr+0) => arr[0]
*(ptr+1) => arr[1]
*(ptr+2) => arr[2]
and so on..
for(i=0;i<n;i++)
{
scanf("%d",(ptr+i));
}
(ptr+i) is equivalent to &arr[i].
This means,
(ptr+0) => &arr[0]
(ptr+1) => &arr[1]
(ptr+2) => &arr[2]
and so on..
Note that, we are not incrementing pointer. So, ptr still points to arr[0] only. So, there is no need to reassign ptr to arr as done in the previous program.
Similarly, to print the values, *(ptr+i) is used instead of *ptr
for(i=0;i<n;i++)
{
printf("%d\n",*(ptr+i));
}
*(ptr+i) is equivalent to arr[i].
This means,
*(ptr+0) => arr[0]
*(ptr+1) => arr[1]
*(ptr+2) => arr[2]
and so on..