PPS Lab · Experiment 26
Display array values in reverse order using a pointer
CArrays, pointers and functionsJNTUH R22 · CS108ES
Aim
To write a C program that reads n integers into an array and displays them in reverse order by moving a pointer backwards from the end of the array.
Algorithm
- Read n. If it is not between 1 and 50, print an error and stop.
- Read the n integers into the array a.
- Set the pointer p to a + n, the position just after the last element.
- While p is greater than a: move p back by one element (
p--) and print the value it points to,*p. - Stop when p reaches a, after printing a[0].
Program
#include <stdio.h>
#define MAX 50
int main(void) {
int a[MAX], 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++) {
if (scanf("%d", &a[i]) != 1) {
printf("Invalid input\n");
return 1;
}
}
printf("Elements in reverse order: ");
int *p = a + n; /* just after the last element */
while (p > a) {
p--; /* step back one element */
printf("%d ", *p);
}
printf("\n");
return 0;
}Output
Five elements
Enter the number of elements (1 to 50): 5
Enter 5 integers: 11 22 33 44 55
Elements in reverse order: 55 44 33 22 11 Negative numbers and zero
Enter the number of elements (1 to 50): 4
Enter 4 integers: -3 8 0 -9
Elements in reverse order: -9 0 8 -3 Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
The pointer starts at a + n, one place after the last element, and is moved back before each value is printed. C allows a pointer to point just past the end of an array (as long as it is not dereferenced there), but not before its start. So this loop never makes p point before a[0].
p-- moves the pointer back by one int, not one byte, so it steps from a[4] to a[3] and so on.
The array itself is not changed; only the order of printing is reversed.
Common mistakes
Starting the pointer at
a + nand printing*pbefore moving it.a + nis past the end of the array, so the first value printed is garbage. Move back first, then print (or start ata + n - 1).Using
while (p >= a)withp--after the print. The lastp--makesppoint before the array, which the C standard does not allow, even if it often seems to work.Forgetting the
*and printingp. That prints an address (and gives a format warning), not the element's value.Reversing the array in place when the task is only to display it in reverse. That changes the data for any code that uses the array afterwards.
Viva questions
Try to answer each one aloud before you open it.
If p points to a[3], where does p - 1 point?
To
a[2]. Subtracting 1 from a pointer moves it back by one element of its type.Why does the loop start at a + n and not at a + n - 1?
So that the loop can stop with the test
p > awithout ever moving the pointer before the start of the array. Each step moves back first, then prints.What does *p mean, and what does p mean?
pis the address stored in the pointer.*pis the value at that address, here an element of the array.Can two pointers be compared with > or <?
Yes, when both point into the same array (or just past its end). The pointer to the later element is the greater one.
How else could you print the array in reverse with a pointer?
With an index loop:
for (i = n - 1; i >= 0; i--) printf("%d ", *(a + i));.