---
title: "Reverse array and display via pointer"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-reverse-array"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-reverse-array
site: BCT Engineering
---

# Reverse array and display via pointer

Computer Programming · Chapter 9: Pointers

- Asked in: 2072 Kartik, 6 marks
- Video: [C Program To Display Elements of Array In Reverse Order using Pointers](https://www.youtube.com/watch?v=CYf-rVrKNTI) — Technotip
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-reverse-array
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

Write a C program that calls reverse array () to reverse the array and return the array and display the element of reversed array using pointer.

## Short answer

Function reverseArray uses two pointers (start and end). While i < j, swap *i and *j, then move pointers inward. It returns the array pointer. Main calls it, then prints reversed elements using *(p+i). Example output: 5 4 3 2 1.

## Answer

**Logic.** `reverseArray(int *a, int n)` uses two pointers: `i` at the first element and `j` at the last (`a + n - 1`). While `i < j` the values `*i` and `*j` are swapped, then `i` moves forward and `j` backward. The function returns the pointer to the array, and `main()` displays the reversed elements with `*(p + i)`.

```c
#include <stdio.h>

/* reverses the array in place and returns its address */
int *reverseArray(int *a, int n)
{
    int *i = a, *j = a + n - 1, t;
    while (i < j) {
        t = *i; *i = *j; *j = t;
        i++; j--;
    }
    return a;
}

int main(void)
{
    int a[50], n, *p;
    printf("Enter N: ");
    scanf("%d", &n);
    printf("Enter %d elements: ", n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    p = reverseArray(a, n);
    printf("Reversed array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", *(p + i));
    printf("\n");
    return 0;
}
```

**Sample run**

Input: `5 1 2 3 4 5`

```text
Enter N: Enter 5 elements: Reversed array: 5 4 3 2 1
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does reverseArray(int *a, int n) reverse the array?
   A. Sorts the array in descending order
   B. Swaps values at pointers moving from both ends until they meet
   C. Copies the array into a new array
   D. Subtracts each element from n
   Answer: B. i starts at the first element, j at the last; values are swapped and the pointers move inward. (Key point: Two-pointer method)

2. Which expression gives the pointer to the last element?
   A. a + n
   B. a + n - 1
   C. a - n
   D. *a + n
   Answer: B. Element n-1 is at a + n - 1; a + n is one past the end. (Key point: Last element address)

3. Why does the loop run while i < j?
   A. So the middle swaps are not undone by swapping back past the middle
   B. To swap every element twice
   C. So that i reaches the end
   D. To avoid using temp
   Answer: A. Once the pointers meet or cross, each pair has already been swapped; continuing would undo the reversal. (Key point: Loop condition)

4. What is displayed after reversing 1 2 3 4 5?
   A. 1 2 3 4 5
   B. 5 4 3 2 1
   C. 2 1 4 3 5
   D. 5 2 3 4 1
   Answer: B. Swapping 1<->5 and 2<->4 leaves 3 in the middle giving 5 4 3 2 1. (Key point: Result for 1 2 3 4 5)

## Related in Computer Programming

- [Second largest element using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-second-largest) — Computer Programming, chapter 9
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Relation between array and pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-pointer-relation) — Computer Programming, chapter 9, asked 5×
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Can a function return an array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-return-array) — Computer Programming, chapter 7
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3

## Related in other subjects

- [Two-element array and end fire pattern](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Atwo-isotropic-endfire-pattern) — Propagation and Antenna, chapter 2
- [Two-element array: broadside and end fire](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Atwo-element-array-broadside-endfire) — Propagation and Antenna, chapter 2
- [PC-to-phone calls via internet and PSTN](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Apc-to-phone-call-internet) — Telecommunications, chapter 8
- [8086 ALP: largest and smallest, display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-largest-smallest-display) — Microprocessor, chapter 3
