I was struggling to fix a code today, then I come across something similar to:
typedef struct {
int a;
int b;
int c;
int d;
char* word;
} mystruct;
int main(int argc, char **argv){
mystruct* structptr = malloc(sizeof(mystruct));
if (structptr==NULL) {
printf("ERROR!")
...
}
...
free(structptr);
return 0;
}
the code was giving lots of memory errors due to the fact, that char* word
is a string of variable length, and malloc was not allocating enough memory for it. In fact it was only allocating 20 Bytes
for the whole struct
. Is there a way around this issue, without turning the char*
into sth like char word[50]
?