Originally Posted by
littlerunaway
I don't quite understand why I need malloc. don't I already assign the memory in the struct definition when I define the dict0 points to 5 characters long array?
If you don't want to use malloc you need a buffer somewhere that you can point to.
Code:
static char buffer[5] = {0};
int main(void){
struct dict{
int length;
char (*dict0)[5];
};
struct dict Mydict;
{
char (*ptr)[5] = &buffer;
Mydict.dict0 = ptr;
}
strcpy(*Mydict.dict0, "test");
printf("*Mydict.dict0: %s\n", *Mydict.dict0);
return 0;
}
If you don't want to use the temporary variable you can also use the typecast
Mydict.dict0 = (char(*)[5])&buffer;
But this looks quite ugly to me. What's wrong with just making your member a char** ?