To avoid making Pattern.context
a fixed size array and to be certain there is enough space to copy into you will need to dynamically allocate memory for Pattern.context
to store a copy of patt
. Either:
use
malloc()
(remembering to allocatestrlen(patt) + 1
for the null terminator) andstrcpy()
:patterns[patternsIndex].context = malloc(strlen(patt) + 1); if (patterns[patternsIndex].context) { strcpy(patterns[patternsIndex].context, patt); }
use
strdup()
:patterns[patternsIndex].context = strdup(patt);
In either case, remember to free()
the string copy when no longer required:
free(patterns[patternsIndex].context);