My function also returns an integer pointer. I was under the impression that array names are just integer pointers, so that's why i declared it like that. I have made a function called reverse() to reverse a pointer and when i run it on my ubuntu 18.04 with gcc version 7.5, I get a segmentation fault core dumped. Why is this and what is the correct and most efficient code for this?
#include <stdio.h>
int * reverse(int * array, int n){
int * rev;
int i = n - 1;
int j = 0;
while(i >= 0){
rev[j] = array[i];
j++;
i--;
}
return rev;
}
int main(){
int array[] = {1,2,3,4};
int * p; //the pointer p that im declaring
p = reverse(array, 4); //i think this is the segmentation error
int i = 0;
printf("the first element is %d", p[0]);
}