Which choice is printed when the following program is executed?
#include <stdio.h>
struct test {
int i;
char *c;
} st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
int main(void) {
struct test *p = st;
p += 1;
++p->c;
printf("%s,", p++->c);
printf("%c,", *++p->c);
printf("%d,", p[0].i);
printf("%s", p->c);
return 0;
}
Detailed Solution
p initially points to st[0]. p += 1 points to st[1] ({4, 'better'}). ++p->c advances 'better' to 'etter'. p++->c prints 'etter' and post-increments p to st[2] ({6, 'jungle'}). ++p->c in *++p->c modifies p->c from 'ancestor' to 'ncestor' and evaluates to 'n' (wait, ++p advances to st[3], c='ancestor' -> ++c is 'ncestor', * is 'n'). Next p[0].i is st[3].i which is 8. p->c is 'ncestor'. So output is 'etter, u, 8, ncestor'.