简体   繁体   中英

Returning a pointer from a function

This is in reference to this question : why pointer to pointer is needed to allocate memory in function

The answer to the question explained why this doesn't work :

    void three(int * p) 
    {
        p = (int *) malloc(sizeof(int));
        *p=3;
    }

    void main()
    {
        int *p = 0;
        three(p);
        printf("%d",*p);
    }

... but this works :

    void three(int ** p) 
    {
        *p = (int *) malloc(sizeof(int));
        **p=3;
    }

    void main()
    {
        int *p = 0;
        three(&p);
        printf("%d",*p);
    }

My question is, this also works, by returning a pointer from the function. Why is that?

    int* three(int * p) 
    {
        p = (int *) malloc(sizeof(int));
        *p=3;
        return p;
    }

    void main()
    {
        int *p = 0;
        p=three(p);
        printf("%d",*p);
    }
int* three(int * p) 
    {
        p = (int *) malloc(sizeof(int));
        *p=3;
        return p;
    }

Because here you're returning a copy of the pointer p and this pointer now points to valid memory, which contains the value 3.

You originally passed in a copy of your p as an argument, so you're not changing the one you passed in, but a copy. Then you return that copy, and assign it.

From the comment, which is a very valid point, this will also work just as well:

 int* three() 
        {
           //no need to pass anything in. Just return it.
            int * p = (int *) malloc(sizeof(int));
            *p=3;
            return p;
        }

They're completely different (and if you truly understand why the first works, you'd see there's no connection).

By returning, you're not attempting to modify the already existing pointer from inside the function. You're just returning a new pointer, and assigning its value outside.

Look at it as a question of scope.

In main() you have a pointer p .

int *p = 0;

p in main is set to NULL. When you make a call to the three function passing it p:

three(p);

You are passing a pointer to NULL. What happens to it is outside the scope of main() . main() does not know, nor does it care what happens. main() only cares about its copy of p , which at this point is still set to NULL. Unless I reassign p within the scope of main() (including handing off the address of p ), p is still just a pointer pointing to NULL.

If I give you this code:

void main()     
{
     int *p = 0;
     funcX(p);
     printf("%d",*p);     
} 

You can tell me definitively what is going to happen (Segmentation fault) without ever knowing what funcX() does because we're passing a copy of the pointer to this function, but a copy doesn't affect the original.

But if I give you this code:

void main()     
{
     int *p = 0;
     funcX(&p);
     printf("%d",*p);     
} 

You can't tell me what will happen unless you know what funcX() is doing.

That make sense?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM