In this article we are going to learn of about the pointer-array concept in c programming.
There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer.
Today we are going to reverse the pointer array in-place which is faster than working with the value variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "stdio.h" void reversearray(int *p, int n) { int *first = p; int *last = p+n-1; while(first<last) { int temp = *first; *first = *last; *last = temp; first++; last--; } printf("Reversed array elements are: "); for(int i=0; i<n; i++) printf("%d ", *p++); } int main() { int n; printf("Enter n: "); scanf("%d", &n); int a[n]; printf("Enter %d numbers: "); for(int i=0; i<n; i++) scanf("%d", &a[i]); reversearray(a, n); return 0; } |
Input and output for the above program is as follows:
Enter n: 5
Enter 5 numbers: 6 7 1 3 8
Reversed array elements are: 8 3 1 7 6
Take your time to comment on this article.