In your posted struct1
, words
is declared like this:
char words[kInputSize];
... and then you try to set it equal to a pointer, here:
block->words = malloc(strlen(words) + 1);
This doesn't work because in C++ (or C for that matter) you are not allowed to set an array equal to a pointer.
If you want to be able to set words
equal to a pointer (as returned by malloc()
) you would need to declare it as a pointer inside the struct, instead:
char * words;
If, on the other hand, you really just want to copy over the string-data from words
into the existing char-array at block->words
, one way to do that would be:
#include <cstring>
[...]
strncpy(block->words, words, sizeof(block->words));
(Note that the sizeof(block->words)
invocation to determine the number of bytes in the char-array only works when block->words
is declared as a char-array; if you change it to char * words
as suggested above, then sizeof(block->words)
will then return either 4 or 8 [depending on the size of a pointer on your system] and that is almost certainly not what you want)