passing pointers in nested functions

This is a discussion on passing pointers in nested functions within the C Programming forums, part of the General Programming Boards category; Since function1 doesn't see the changes either , your question makes no sense. Code: Code: include <stdio.h> #include <stdlib.h> void ...

  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since function1 doesn't see the changes either, your question makes no sense.

    Code:
    Code:
    include <stdio.h>
    #include <stdlib.h>
    
    void function2(int *ptr) {
        ptr = malloc(5 * sizeof(int));
        printf("function2 just happened to ptr %p.\n", ptr);
    }
    
    void function1(int *ptr) {
        printf("ptr is %p.\n", ptr);
        function2(ptr);
        printf("ptr is now %p.\n", ptr);
    }
    
    int main(void) {
        int *bob = NULL;
        printf("bob initialized to %p.\n", bob);
        function1(bob);
        printf("bob is finally %p.\n", bob);
        return 0;
    }
    Output:
    Code:
    $ ./temp
    bob initialized to 0x0.
    ptr is 0x0.
    function2 just happened to ptr 0x100160.
    ptr is now 0x0.
    bob is finally 0x0.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Since function1 doesn't see the changes either, your question makes no sense.
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    struct _node {
            int X;
            struct _node *next;
    };
    typedef struct _node node;
    
    //struct _node *testfunc2 ...
    void testfunc2 (struct _node *ptr) {
            node *here=malloc(sizeof(*here));
            here->X=12;
            ptr=here;
            if (ptr!=NULL) puts("arghh.");
    }    
    
    void testfunc (struct _node *ptr) {
            testfunc2(ptr);
            //ptr=testfuction2(ptr);
            if (ptr!=NULL) puts("yes");
    }
    
    int main () {
            struct _node *ptr=NULL;
            testfunc(ptr);
            if (ptr!=NULL) puts("YES");
            return 0;
    }
    Output:
    Code:
    arghh.
    I think I must have been doing that thing in red there.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    I would just be aware that you are neither freeing your allocated memory, nor saving the pointer itself, so it's two memory leaks, so to speak.
    Since the main function needs that memory, you need to pass a T** to get that value in that pointer back to main, and thus free it properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by MK27
    I noticed today that a pointer declared in main, when passed to function that passes to a function, will not change regardless of what these functions do -- altho the changes in the second function affect the first function (but not main) whereas one passed to a function that changes it will. Am I off my rocker? Why is this?
    A simpler example:
    Code:
    #include <stdio.h>
    
    void foo(int x, int *p)
    {
        printf("Before assigning to p in foo: &#37;p\n", (void*)p);
        p = &x;
        printf("After assigning to p in foo: %p\n", (void*)p);
    }
    
    int main()
    {
        int x = 123;
        int *p = 0;
        printf("Before calling foo in main: %p\n", (void*)p);
        foo(x, p);
        printf("After calling foo in main: %p\n", (void*)p);
        return 0;
    }
    The reason is that pointers are passed by value, so if you change the local pointer in the function, the change is not reflected in the caller. You have to pass a pointer to a pointer instead. Of course, if you change what the pointer points to, then the change is reflected in the caller, since the caller's pointer points to the same object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and like int, if you want a pointer to be changed inside a function, you need to pass a pointer to the pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That's because the value of the pointer, i.e. the address it holds is copied by value. Remember that everything in C is copied by value and pointers just allow you to pass around an address, which can then be dereferenced to get to the real value.
    What happens is that in your function1, ptr is a variable, which you pass to function2, where it is assigned. When function2 returns, ptr holds the address, but because ptr is a copy of the original variable (also unfortunately named ptr) that was passed to function1, it will have no effect on the ptr in main.

    QuantumPete
    wtf? How can my reply come before the original post???
    Last edited by QuantumPete; 12-04-2008 at 02:20 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    passing pointers in nested functions

    I noticed today that a pointer declared in main, when passed to function that passes to a function, will not change regardless of what these functions do -- altho the changes in the second function affect the first function (but not main) whereas one passed to a function that changes it will. Am I off my rocker? Why is this?

    Code:
    int main() {
         struct _node *ptr=NULL;
         function1(ptr);
         //ptr still NULL
    }
    function1(struct _node *ptr){
         function2(ptr);
         //ptr reflects malloc & assignment
    }
    function2(struct _node *ptr{ 
         malloc and assign ptr.
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing lists to functions
    By Griever in forum C Programming
    Replies: 23
    Last Post: 11-14-2008, 04:26 PM
  2. Passing Pointers to Functions question..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 10-18-2008, 03:51 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing functions with variable
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 10:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21