Just wondering if someone can clear things up for me a little. I'm fairly new to C/C++ and just getting in a bit of a tangle with pointers.
My problem is I have a vector of a defined structure, which needs to store some characters. When I try and print out these characters later, it appears to be uninitialised. I believe the problem is because I'm using a char*, who's memory contents then just gets discarded later on, and hence I can't get the characters anymore.
e.g.
[CODE="cpp"]
typedef struct _info{
char* label;
} Info;[/CODE]
a little later on some parsing takes place...
[CODE="cpp"]
char *value;
for (....)
{
char *label;
Info info;
label = value;
info.label = label;
// info gets added to a vector
}
[/CODE]
When I come to print info later I get ▌▌▌▌▌▌▌▌▌▌
Is my understanding of this correct? If so, then I guess that label within the Info structure needs to be a char[]. Is there a simple way to then copy the 'value' within the for loop into this char array as my attempts have just led to compiler errors... ?
Thanks very much
My problem is I have a vector of a defined structure, which needs to store some characters. When I try and print out these characters later, it appears to be uninitialised. I believe the problem is because I'm using a char*, who's memory contents then just gets discarded later on, and hence I can't get the characters anymore.
e.g.
[CODE="cpp"]
typedef struct _info{
char* label;
} Info;[/CODE]
a little later on some parsing takes place...
[CODE="cpp"]
char *value;
for (....)
{
char *label;
Info info;
label = value;
info.label = label;
// info gets added to a vector
}
[/CODE]
When I come to print info later I get ▌▌▌▌▌▌▌▌▌▌
Is my understanding of this correct? If so, then I guess that label within the Info structure needs to be a char[]. Is there a simple way to then copy the 'value' within the for loop into this char array as my attempts have just led to compiler errors... ?
Thanks very much
Comment