PPS Lab · Experiment 25
Read array elements using a pointer
CArrays, pointers and functionsJNTUH R22 · CS108ES
Aim
To write a C program that reads n integers into an array through a pointer and then displays the values using ordinary array indexing.
Algorithm
- Declare an integer array a and an integer pointer p.
- Read n. If it is not between 1 and 50, print an error and stop.
- Set p to point to the first element of the array: p = a.
- For i from 0 to n − 1, read a value into the address p + i.
- For i from 0 to n − 1, print a[i] using the array name and index.
Program
#include <stdio.h>
#define MAX 50
int main(void) {
int a[MAX];
int *p = a; /* p points to a[0] */
int n;
printf("Enter the number of elements (1 to %d): ", MAX);
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
printf("Invalid number of elements\n");
return 1;
}
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
/* p + i is the address of a[i], so no & is needed */
if (scanf("%d", p + i) != 1) {
printf("Invalid input\n");
return 1;
}
}
printf("Array elements (displayed using the array):\n");
for (int i = 0; i < n; i++) {
printf("a[%d] = %d\n", i, a[i]);
}
return 0;
}Output
Enter the number of elements (1 to 50): 5
Enter 5 integers: 10 20 30 40 50
Array elements (displayed using the array):
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
The name of an array, used in an expression, gives the address of its first element, so int *p = a; makes p point to a[0].
Adding i to a pointer moves it forward by i elements, not i bytes, so p + i is the address of a[i] (the same as &a[i]). That is why scanf gets p + i with no &.
The values were stored through p, but printing a[i] shows them, because p and a refer to the same memory.
Common mistakes
Writing
scanf("%d", &(p + i))orscanf("%d", *(p + i)).p + iis already an address.&of it is a compile error, and*(p + i)passes the value, not the address.Declaring
int *p;without setting it toa. An uninitialised pointer points to an unknown place, and writing through it can crash the program.Writing
scanf("%d", p++)and then printing throughplater. After the loopppoints past the last element, so it must be reset toabefore it is used again.Thinking
p + 1moves one byte. It moves oneint(usually 4 bytes), because pointer arithmetic is done in units of the pointed-to type.
Viva questions
Try to answer each one aloud before you open it.
What is a pointer?
A variable that stores the address of another variable.
int *pdeclares p as a pointer to anint.What is the relation between an array name and a pointer?
In an expression, the array name gives the address of its first element. So
a[i]is the same as*(a + i), and&a[i]is the same asa + i.If p points to a[0], what are p + 2 and *(p + 2)?
p + 2is the address ofa[2], and*(p + 2)is the value stored ina[2].What is the difference between the & and * operators?
&gives the address of a variable.*(dereference) gives the value stored at an address.Can you write a = a + 1 for an array a?
No. An array name is not a variable that can be changed; it always refers to the same memory. A pointer variable like p can be changed with
p = p + 1.