Experiment 10 – Array Reverse Using Pointers
Aim
To reverse the elements of an array using pointers and pointer arithmetic in C programming. Learn how to manipulate array elements through pointers and swap values using pointer dereferencing. This experiment helps students understand pointer operations, memory addresses, and efficient in-place array manipulation.
Through this experiment, students will learn to use pointers for array traversal, understand pointer arithmetic, implement efficient in-place algorithms, and gain deeper insight into how arrays and pointers work in C.
Learning Outcomes
After completing this experiment, students will be able to:
- Use pointers to access and manipulate array elements
- Understand pointer arithmetic (increment, decrement, addition)
- Implement in-place array reversal using two pointers
- Swap values using pointer dereferencing
- Understand the relationship between arrays and pointers
- Use pointer comparison to control loop termination
- Implement efficient algorithms using pointers
- Understand memory addresses and pointer operations
Algorithm
The algorithm for reversing an array using pointers uses two pointers moving toward each other:
Algorithm Steps:
- Initialize Pointers: Set
start = arr(points to first element) andend = arr + n - 1(points to last element) - Loop Condition: Continue while
start < end(pointers haven't met) - Swap Values:
- Store value at start:
temp = *start - Copy end to start:
*start = *end - Copy temp to end:
*end = temp
- Store value at start:
- Move Pointers: Increment start (
start++), decrement end (end--) - Repeat: Continue until pointers meet or cross
Efficiency: This algorithm is O(n) time complexity and O(1) space complexity (in-place), making it very efficient. It requires n/2 swaps to reverse an array of n elements.
Procedure
- Include
stdio.hheader file - Declare an array and pointer variables
- Read array size and elements
- Initialize two pointers:
startpointing to first element (arr or &arr[0])endpointing to last element (&arr[n-1])
- Swap elements using pointers:
- Use temporary variable to swap *start and *end
- Increment start pointer
- Decrement end pointer
- Continue until start >= end
- Display the reversed array
Flowchart
START │ ├─→ Declare: arr[], n, *start, *end, temp │ ├─→ Read n ├─→ Read array elements │ ├─→ start = arr (or &arr[0]) ├─→ end = arr + n - 1 (or &arr[n-1]) │ ├─→ WHILE (start < end) │ │ │ ├─→ temp = *start │ ├─→ *start = *end │ ├─→ *end = temp │ ├─→ start++ │ └─→ end-- │ ├─→ Display reversed array │ └─→ END
Program Code
#include <stdio.h>
// Function to reverse array using pointers
void reverseArray(int *arr, int n) {
int *start = arr; // Pointer to first element
int *end = arr + n - 1; // Pointer to last element
int temp;
// Swap elements until pointers meet
while (start < end) {
// Swap values at start and end pointers
temp = *start;
*start = *end;
*end = temp;
// Move pointers towards center
start++;
end--;
}
}
int main() {
int arr[100], n, i;
int *ptr;
// Input array size
printf("Enter the number of elements: ");
scanf("%d", &n);
// Input array elements
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Display original array
printf("\nOriginal array: ");
ptr = arr; // Pointer points to first element
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i)); // Using pointer arithmetic
}
printf("\n");
// Reverse array using pointers
reverseArray(arr, n);
// Display reversed array
printf("Reversed array: ");
ptr = arr;
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
printf("\n");
return 0;
}
// Alternative: Using array notation with pointers
/*
void reverseArray(int arr[], int n) {
int *start = &arr[0];
int *end = &arr[n-1];
int temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
*/Sample Input and Output
Sample 1:
Input:
Enter the number of elements: 5 Enter 5 elements: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50
Output:
Original array: 10 20 30 40 50 Reversed array: 50 40 30 20 10
Sample 2:
Input:
Enter the number of elements: 7 Enter 7 elements: Element 1: 1 Element 2: 2 Element 3: 3 Element 4: 4 Element 5: 5 Element 6: 6 Element 7: 7
Output:
Original array: 1 2 3 4 5 6 7 Reversed array: 7 6 5 4 3 2 1
Sample 3:
Input:
Enter the number of elements: 4 Enter 4 elements: Element 1: 15 Element 2: 25 Element 3: 35 Element 4: 45
Output:
Original array: 15 25 35 45 Reversed array: 45 35 25 15
Use Case / Real-world Relevance
Pointers and array reversal operations are fundamental in programming:
- Memory Efficiency: Pointers allow direct memory access without copying data
- String Processing: Reversing strings, palindromes, text manipulation
- Data Structures: Linked lists, stacks, queues use pointer-based operations
- Algorithm Implementation: Many algorithms require in-place array reversal
- System Programming: Low-level operations, device drivers, memory management
- Performance Optimization: Pointer arithmetic is faster than array indexing in some cases
- Function Parameters: Passing arrays to functions efficiently using pointers
- Dynamic Memory: Understanding pointers is essential for malloc, calloc, and dynamic allocation
Mastering pointers is crucial for advanced C programming. Pointers provide direct memory access and are essential for understanding how data structures and memory management work in C. The two-pointer technique used in array reversal is a fundamental pattern used in many algorithms including palindrome checking, two-sum problems, and efficient array manipulation.
Viva Questions
Q1: What is pointer arithmetic and how does it work with arrays?
Pointer arithmetic allows adding/subtracting integers to pointers. When you increment a pointer, it moves by the size of the pointed type. For int*, incrementing moves by sizeof(int) bytes. Arrays and pointers are closely related: array name is a pointer to first element, so arr[i] is equivalent to *(arr + i).
Q2: Why do we use two pointers (start and end) instead of one?
Two pointers moving toward each other allow in-place reversal with O(1) extra space. With one pointer, you'd need O(n) extra space to store reversed array. Two-pointer technique swaps elements from both ends simultaneously, meeting in the middle, requiring only n/2 iterations instead of n.
Q3: What happens if we continue the loop when start >= end?
If we continue when start >= end, we'd swap elements that have already been swapped, effectively reversing the array back to original. For even-length arrays, pointers meet (start == end) at center, no swap needed. For odd-length, they cross. The condition start < end ensures we stop at the right point.
Q4: Can we reverse an array without using a temporary variable?
Yes, using XOR swap: *start ^= *end; *end ^= *start; *start ^= *end;. However, this is less readable and doesn't work if both pointers point to same element. Temporary variable approach is clearer and more reliable. Modern compilers optimize temporary variable swaps efficiently.
Q5: What is the difference between *ptr++ and (*ptr)++?
*ptr++ dereferences ptr, returns value, then increments pointer (post-increment pointer).(*ptr)++ dereferences ptr, increments the value pointed to, pointer unchanged. Operator precedence: * and ++ have same precedence, associate right-to-left, so *ptr++ is *(ptr++), not (*ptr)++.
Q6: How would you reverse only a portion of an array using pointers?
Initialize start to beginning of portion, end to end of portion. Use same algorithm but with modified bounds. For example, to reverse elements from index i to j: start = arr + i, end = arr + j, then apply same reversal logic. This demonstrates the flexibility of pointer-based approaches.
Q7: What is the advantage of using pointers over array indexing for this operation?
Pointers can be more efficient (fewer address calculations), more flexible (can pass to functions easily), and enable direct memory manipulation. However, for this specific case, both are similar in performance. Modern compilers optimize array indexing well. Pointers are more idiomatic for this two-pointer pattern and make the algorithm's intent clearer.
Q8: Can we use this technique to reverse a string?
Yes, same technique works for strings (character arrays). Initialize start to first character, end to last character (before null terminator). Swap characters until pointers meet. Important: don't swap the null terminator, so end should point to last character, not the null. This is an efficient way to reverse strings in-place.
Related Links
🔹 Author: Dr. J. Siva Ramakrishna
🔹 Institution: Narayana Engineering College, Gudur
🔹 Last Updated: 9 January 2026