1

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.

3
  • Where does the pointer returned by func point? Commented Mar 25, 2014 at 19:07
  • @Broseph p = func(x,9); maybe? I'm not sure. Commented Mar 25, 2014 at 19:10
  • Look at the code for func, and follow the pointer arr. What happens to it? Commented Mar 25, 2014 at 19:10

3 Answers 3

4

Your problem is with recursion, not pointers. Visualizing the recursive calls, the returned pointer points to the 5th element:

func([1, 2, 3, 4, 5, 6, 7, 8, 9], 9) ->
func([2, 3, 4, 5, 6, 7, 8, 1], 7) ->
func([3, 4, 5, 6, 7, 2, 1], 5) ->
func([4, 5, 6, 3, 2, 1], 3) ->
func([5, 4, 3, 2, 1], 1) ->
[5, 4, 3, 2, 1]

From the comments:

This might illustrate it better, I incremented the array before printing, so you can see what the array looks like in the recursive call: http://ideone.com/lzgEUX

Sign up to request clarification or add additional context in comments.

6 Comments

But after 1st iteration shouldn't it be func([9, 2, 3, 4, 5, 6, 7, 8, 1], 7). If not then how come this is happening ideone.com/62HcYW ?
@tintinmj that is what is happening to the array, but the pointer that is passed around is being incremented to point the next item in the array.
But what about after the 1st recursion? where did 9 go?
@tintinmj subsequent recursive calls skip over the head of the array by moving the pointer one element forward each time. Then when the base case is reached, the returned pointer points to the middle of the array because that's where the algorithm ends. You can test this by changing the print line to printf("%d\n",*((p-4)+i));
@tintinmj This is never called: func([9, 2, 3, 4, 5, 6, 7, 8, 1], 7), the 9 is skipped over before the call (with arr++). This might illustrate it better, I incremented the array before printing, so you can see what the array looks like in the recursive call: ideone.com/lzgEUX
|
1

Check out this code:

#include <stdio.h>

int* func(int* arr, int n){
     if (n==1 || n==0)
        return arr; //remember this is the pointer we've been incrementing
     int temp = *arr; //instead of using bracket, I'm translating to pointer
     *arr = *(arr + n - 1); //arithmetic so it is more clear that we are 
     *(arr + n - 1) = temp; //merely manipulating a pointer to an int.
     return func(arr + 1, n - 2); //the pointer is incremented!
}


int main(int argc, char** args){
    int x[]={1,2,3,4,5,6,7,8,9}; //initialize our array
    int i=-4; //this is how much we need to offset the returned pointer
    int* p = func(x,9); //get the returned pointer
    for(;i<5;i++)
        printf("%d\n", *(p+i)); //print out each element of the array
    return 0;
}

Comments

1

So when the recursive algorithm is done, fund will return a pointer to the 5th element in arr. So your print-sentence: printf("%d\n",*(p+i));, will start printing from the 5th element, and 9 fields forward. When it tries print elements after the 9th element, you will get garbage values.

I tried to visualize it like this, maybe it helps:

arr = 0         // as non-relative pointer to element
temp = 1
arr[0] = 9
arr[8] = 1
func( arr=1, n = 7 )
    arr = 1
    temp = 2
    arr[1] = 8
    arr[7] = 2
    func ( arr=2, n = 5 )
        arr = 2
        temp = 3
        arr[2] = 7
        arr[6] = 3
        func ( arr=3, n = 3 )
            arr = 3
            temp = 4
            arr[3] = 6
            arr[5] = 4
            func ( arr=4, n = 1 )
                arr = 4
                n == 1
                return arr = 4
            return 4
        return 4
    return 4
return 4
p = 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.