ProgrammingHard
struct P { char a; int b; }; printf("%zu", offsetof(struct P, b)); What is printed with 4-byte int and natural alignment (stddef.h included)?
- A. 1
- B. 4
- C. 2
- D. 3
Correct answer: B. 4
The int must start at a multiple of 4, and the char a occupies offset 0, so b starts at offset 4.
More C Programming questions
- struct R { int v; struct R next; }; struct R c = {3, NULL}, b = {2, &c}, a = {1, &b}; printf("%d", a.next->next->v); What is printed?
- struct Pt { int x, y; }; struct Pt pts[3] = {{1, 2}, {3, 4}, {5, 6}}; struct Pt p = pts + 1; printf("%d %d", p->x, (p - pts)); What is…
- struct Item { int id; int qty; }; struct Item a[2] = {{1, 5}, {2, 7}}; struct Item b = a[1]; b.qty = 0; printf("%d %d", a[1].qty, b.qty)…
- struct Item { int id; int qty; }; struct Item a[3] = {{1, 2}, {3, 4}}; printf("%d", a[2].qty); What is printed?
- struct S { int key; int val; }; struct S arr[4] = {[2] = {7, 70}, {8, 80}}; printf("%d", arr[3].val); What is printed?
- struct S { int key; int val; }; struct S arr[4] = {[2] = {7, 70}, {8, 80}}; printf("%d", arr[1].key); What is printed?