Skip to lab content

UNIT 03 · Arrays, Pointers and Functions

Display an array in reverse using a pointer

EXERCISE 03DC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a program for display values reverse order from an array using a pointer.
Input format & conventions

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

  1. Read and validate the array.
  2. Set p = a + n, one position past the final element.
  3. While p > a, move p back one element.
  4. Print the value at *p.
  5. 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.

Flowchart for Display an array in reverse using a pointer: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#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;
}
Open in compiler ↗

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
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Array 10,20,30; p = a+3Decrement to a+2Print 30
p = a+2Decrement to a+1Print 20
p = a+1Decrement to aPrint 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

INPUT
3
10 20 30
OUTPUT
Reverse: 30 20 10

Sample 2

INPUT
1
-4
OUTPUT
Reverse: -4

Sample 3

INPUT
4
1 2 2 1
OUTPUT
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.