THE SYLLABUS QUESTION
What you need to solve
Write a program for display values reverse order from an array using a pointer.
Array size n (1–100), followed by n integers between -1,000,000 and 1,000,000.
UNDERSTAND THE IDEA
Explanation
The program displays the array backward without changing its stored order. Start at the permitted one-past-the-end pointer a + n, then decrement before dereferencing.
The loop stops at the beginning of the array. It never forms or dereferences a pointer before a[0]. This matters because even forming an out-of-bounds pointer is unsafe in C.
PLAN BEFORE CODING
Algorithm
- Read and validate the array.
- Set p = a + n, one position past the final element.
- While p > a, move p back one element.
- Print the value at *p.
- Stop after printing the first element.
SEE THE CONTROL FLOW
Flowchart
Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int a[MAX_SIZE], n;
if (scanf("%d", &n) != 1 || n < 1 || n > MAX_SIZE) {
puts("Invalid input."); return 1;
}
for (int i = 0; i < n; ++i) {
if (scanf("%d", &a[i]) != 1 || a[i] < -1000000 || a[i] > 1000000) {
puts("Invalid input."); return 1;
}
}
int *p = a + n;
printf("Reverse: ");
while (p > a) {
--p;
printf("%d%s", *p, p == a ? "" : " ");
}
putchar('\n');
return 0;
}
Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.
Compile and run locally
gcc -std=c17 pointer-array-reverse.c -o lab
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| Array 10,20,30; p = a+3 | Decrement to a+2 | Print 30 |
| p = a+2 | Decrement to a+1 | Print 20 |
| p = a+1 | Decrement to a | Print 10; stop |
CHECK THE BEHAVIOR
Sample input & output
Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.
Sample 1
3
10 20 30
Reverse: 30 20 10
Sample 2
1
-4
Reverse: -4
Sample 3
4
1 2 2 1
Reverse: 1 2 2 1
Common mistakes
- A one-past-the-end pointer may be formed but must not be dereferenced.
- Do not decrement below the first element after the final print.
WHY THIS GROWTH RATE?
Time and space complexity
Time O(n); O(n) array storage and O(1) additional working space.
Reading n elements takes O(n). Starting at a + n, the reverse loop moves the pointer backward exactly n times and prints one value at each step.
Input and output costs add: O(n) + O(n) = O(n). Moving in reverse does not change the number of visited elements. The array uses O(n) storage, and the pointer and counters use O(1) additional space; no reversed copy is created.
Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.