-
Pointer issue
Hello guys,
Am I missing anything here?
Code:
struct whatever
{
char data[100];
};
void something(struct whatever *wp, char **data)
{
*data = wp->data;
return;
}
void get_it(struct whatever *wp)
{
char *stuff = NULL;
something(wp, &stuff);
printf("The stuff is: %s\n", stuff);
return;
}
Is something(); prone to some kind of error? Like something being corrupted, etc leading to `stuff' being corrupted too somehow...
Thanks in advance.
Best Regards.
-
In your program as given, stuff can not be corrupted because stuff never has a valid value to begin with. If you plan to put something into the location pointed to by stuff, you must first acquire actual space for stuff to point to (using malloc).
-
I see nothing wrong in the code you have just posted.
There are potential problems with returning or fetching pointers to local variables, if the struct whatever "goes away" because you leave the function it is local to, or if you free it's memory, you can still have a pointer to the contents of the struct - but the struct is no longer there, so you are pointing "in the weeds". This can cause hideous problems where code randomly works sometimes and not other times (depending on what else has happened to the "weeds").
--
Mats