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