I have this code to take a string of the form bla_2
and separate it:
void separate(char* str, char* word, int* n) {
int i = 0;
while(str[i] != '_') {
word[i] = str[i++];
}
*n = str[++i] - '0';
}
I got:
warning: operation on āiā may be undefined [-Wsequence-point]
But I am only changing i
via ++
operator, I am not assigning anything to.
So, why is the UB, if it is? If not, how to get rid of the warning?
Notice that in my opinion, this question handles a different issue.
word[i] = str[i++];
is a problem.It is compiler's choice if
i
inword[i]
is access before or afteri
is incremented instr[i++];
Do the
i++
after the assignmentFurther
while(str[i] != '_')
likely should beto prevent buffer overrun.