C Strings in Structs -
i want struct carry string. defined so:
typedef struct mystruct { char* stringy } mystruct
and function
free(char *stringy){ //structobj struct object structobj->stringy = stringy }
is correct? have feeling since it's local variable, stringy lost, , pointer point garbage.
sorry, new c.
it garbage if somehow using char** stringy
, structobj->stringy = stringy
means "you know thing stringy
points to? structobj->stringy
points that". of course, still possible unset value pointer pointing to, , @ point dereferencing yield garbage.
here's example make clearer:
#include<stdio.h> typedef struct mstruct { char* stringy; } mystruct; mystruct * structobj; void dosomething(char* stringy) { structobj->stringy = stringy; } int main(int argc, char* argv) { char* = "abc\n"; structobj = malloc(sizeof(mystruct)); dosomething(a); = "qxr\n"; printf(structobj->stringy); }// prints "abc\n"
Comments
Post a Comment