typedef struct
{
int A;
int B;
char* C; // problem is here
}foo;
int SetA(void)
{
....
return retval;
}
int SetB(void)
{
....
return retval;
}
const char* bar(void)
{
.....
char* retval="return val";
.....
return retval;
}
void SetFoo(foo* paramFoo)
{
paramFoo->A = SetA();
paramFoo->B = SetB();
paramFoo->C = bar();
}
static foo staticFoo;
void main()
{
SetFoo(&staticFoo);
printf("%s",staticFoo.C);// printing
}
everything will go fine but the “char*C” in struct foo will not be written well. why? i need to know if i did mistake and how too correct it?. i’ve ran the bar() and it returns the correct value.
Thank you
~ Max
I would prefer using
mallocand laterfreeit, so that I dont have to worry about when the read only string literal get destroyed.Remember to
freethe return value when no longer needed.