This question is about passing buffer positions to sscanf using an incrementing index. To make it clear, there's two code snippets below, one works fine, other don't.
The working code is this:
sscanf(line, "%x %x %x %x %x %x %x %x - %x %x %x %x %x %x %x %x",
&buf[0], &buf[1], &buf[2], &buf[3],
&buf[4], &buf[5], &buf[6], &buf[7],
&buf[8], &buf[9], &buf[10], &buf[11],
&buf[12], &buf[13], &buf[14], &buf[15]);
The not working code is this:
buf_idx = 0;
sscanf(line, "%x %x %x %x %x %x %x %x - %x %x %x %x %x %x %x %x",
&buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++],
&buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++],
&buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++],
&buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++], &buf[buf_idx++]);
The second snipped doesn't rise any segmentation fault, it just doesn't load the values as expected.
I am fully aware that the second snipped is bad C and it's usage is not recommended, for academic proposes I'd like to ask the rationale behind the not working code, why is it Unspecified Behavior.