#121229 - LOst? - Sat Mar 10, 2007 5:17 pm
I have programmed a lot of C/C++ applications for years. So I have encountered this problem a few times. Sure I can handle it, but I want to know why it works this way.
So have a look:
You all know in C, you can send a pointer of a structure as an argument. And it will survive. But why can't my name pointer survive?
My temporary fix for these problems is declaring GetName like this:
However this is a C++ syntax. What if I want to go back to C? That's why I want some help.
_________________
Exceptions are fun
So have a look:
Code: |
char global_name [] = "Peter"; int GetName (char* name) { name = global_name; // "name" is now pointing to &global_name [0] return 1; } int main (void) { char* name = NULL; GetName (name); // Now "name" == NULL! Why didn't it survive past GetName()? } |
You all know in C, you can send a pointer of a structure as an argument. And it will survive. But why can't my name pointer survive?
My temporary fix for these problems is declaring GetName like this:
Code: |
int GetName (char*& name); |
However this is a C++ syntax. What if I want to go back to C? That's why I want some help.
_________________
Exceptions are fun