c - strtok_r behaviour and pointers -
i have piece of code when compiled gcc-4.5.1 x86_64 fails following warning:
error: ctx1 may used uninitialized in function here code snippet:
int args_tokenize(struct kvargs *kvlist, const char *params) { char *str, *ctx1; str = strdup(params); ... while ((str = strtok_r(str, kvargs_pairs_delim, &ctx1)) != null) { /* here goes logic */ ... } return 0; } so reason strtok_r() doesn't ctx1 isn't initialized, although according 'man strtok_r':
the saveptr argument pointer char * variable used internally strtok_r() in order maintain context between successive calls parse same string.
on first call strtok_r(), str should point string parsed, , value of saveptr ignored. in subsequent calls, str should null, , saveptr should unchanged since previous call.
so understand it, should not care if 'saveptr' has sane value or not. wrong?
Comments
Post a Comment