📍📚Intermediate
Sort an Array Using Pointers
Pointer-based sorting
Sorting using pointers demonstrates how to manipulate array elements using pointer arithmetic and dereferencing instead of array subscripts.
C Program to Sort Array Using Pointers
sort_using_pointers.c
C
1#include <stdio.h>23void bubbleSortPointers(int *arr, int n) {4 for (int i = 0; i < n - 1; i++) {5 for (int j = 0; j < n - i - 1; j++) {6 // Compare using pointers7 if (*(arr + j) > *(arr + j + 1)) {8 // Swap using pointers9 int temp = *(arr + j);10 *(arr + j) = *(arr + j + 1);11 *(arr + j + 1) = temp;12 }13 }14 }15}1617int main() {18 int arr[] = {64, 34, 25, 12, 22, 11, 90};19 int n = sizeof(arr) / sizeof(arr[0]);20 21 printf("Original array: ");22 for (int *p = arr; p < arr + n; p++) {23 printf("%d ", *p);24 }25 26 bubbleSortPointers(arr, n);27 28 printf("\nSorted array: ");29 for (int *p = arr; p < arr + n; p++) {30 printf("%d ", *p);31 }32 printf("\n");33 34 return 0;35}Output
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
📝 Pointer vs Array Notation
Array notation:
arr[j]
Pointer notation:
*(arr + j)
🎯 Key Takeaways
✓arr[i] ≡ *(arr + i)
✓Arrays decay to pointers in functions
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials