Thread: Problems with pointers

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    Problems with pointers

    Hi All,

    I've got a program i've been working on for quite a while and have run into a problem with pointers that i'm not able to solve.

    I put together a bare bones example of the problem.

    Check this code out.... it's simple Win32 Console App.

    // pointertest.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <string.h>
    #include <stdlib.h>

    typedef struct node {
    struct node *left;
    struct node *right;
    char textinfo[20];
    } MYnode;

    bool load_pointer(MYnode *);

    int main(int argc, char* argv[])
    { MYnode *t;

    t=NULL;

    if(load_pointer(t))
    { if(t != NULL)
    printf("Inside main %s \n",t->textinfo);
    else
    printf("Crap it didn't work \n");
    }
    return 0;
    }

    bool load_pointer(MYnode *tmp)
    {
    MYnode *p;

    if((p = (MYnode *) malloc (sizeof(*p)))==0)
    { printf("ERROR: We are out of memory!\n");
    return(0);
    }
    else
    { strcpy(p->textinfo,"Hello baby!");
    printf("Inside load_pointer %s \n",p->textinfo);
    tmp=p;
    return(1);
    }
    }


    My problem is with the MYnode *t pointer in function main. I'm not able to persist the pointer information that gets created in the load_pointer function back to the variable t in function main. If you run the code... you'll see that the printf works properly in load_pointer, however the printf in function main never fires.

    I've found a couple of screwy ways around this that i'm not thrilled about. I need the load_function to behave exactly the way I have created it, i.e. returning a bool and the pointer data is passed by ref back to the calling function via parameter. I know i'm missing something basic with this, i've tried passing and address into load_pointer and all sorts of other tricks, pointer to a pointer, etc.

    Can anyone enlighten me as to what the heck i'm missing here?

    Thanks for your help,

    Robert

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    bool load_pointer(MYnode **tmp) 
    { 
    MYnode *p; 
    
    if((p = malloc (sizeof(*p)))==0) 
    { printf("ERROR: We are out of memory!\n"); 
    return(0); 
    } 
    else 
    { strcpy(p->textinfo,"Hello baby!"); 
    printf("Inside load_pointer %s \n",p->textinfo); 
    *tmp=p; 
    return(1); 
    } 
    }
    Then to call use:
    Code:
    if(load_pointer(&t))
    Last edited by swoopy; 10-29-2003 at 08:06 PM.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please don't bypass the code tags again. Read this if you don't understand what they're about:
    http://cboard.cprogramming.com/showt...threadid=25765
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 04:38 PM
  3. Class Pointers
    By ventolin in forum C++ Programming
    Replies: 8
    Last Post: 04-04-2004, 06:07 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM