I have a function(from a tutorial)
int* func(int *arr, int n){
if(n==1 || n==0)
return arr;
else{
int temp=arr[0];
arr[0]=arr[n-1];
arr[n-1]=temp;
return func(++arr,n-2);
}
}
I dry run it and get that it will reverse the array, very good. I'm getting the result as expected when I using this piece of code
int x[]={1,2,3,4,5,6,7,8,9};
int i;
func(x,9);
for(i=0;i<9;i++)
{
printf("%d\n",x[i]);
}
But getting garbage value when using below code
int x[]={1,2,3,4,5,6,7,8,9};
int* p;
p = func(x,9);
for(i=0;i<9;i++)
{
printf("%d\n",*(p+i));
}
Weak in pointer please explain with you answer.
p = func(x,9);maybe? I'm not sure.