ProgrammingVery hardProduct company OA
Assume 4-byte int with 4-byte alignment. What is printed?
Assume 4-byte int with 4-byte alignment. What is printed?
struct P { char tag; int v; };
struct P a[4] = {{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}};
struct P *p = a + 1, *q = &a[3];
char *c = (char *)q - sizeof(struct P);
printf("%td %td %d %c", q - p, (char *)q - (char *)p, ((struct P *)c)->v, (p + 1)->tag);- A. 2 10 3 c
- B. 16 16 3 c
- C. 2 16 3 c
- D. 2 16 2 b
Correct answer: C. 2 16 3 c
Pointer subtraction is measured in elements of the pointed-to type: q − p = 2 structs, but after casting to char * the same distance is 2 × sizeof(struct P) = 16 bytes (1 char + 3 padding + 4 int = 8 each). Stepping back one struct size in bytes from &a[3] lands exactly on a[2], whose v is 3; (p + 1) is also a[2], tag 'c'. The option with 10 uses an unpadded size of 5.
More C Programming questions
- Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed? c char c = 'xff'; printf("%d %d %d %d", c == 0xFF…
- Read the declaration: c char ((x)[4])(int); What is x?
- What is printed? c char a[8] = "abcdefg"; memmove(a + 2, a, 4); char b[6] = "XXXXX"; strncpy(b, "hi", 4); char c[4]; strncpy(c, "wxyz"…
- What is printed? c struct N { int v; struct N next; }; struct N n[5] = {{1, n + 1}, {2, n + 2}, {3, n + 3}, {4, n + 4}, {5, 0}}; struct N…
- What is printed? c int lb(const int a, int n, int x) { int lo = 0, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (a[mid] < x)…
- What is printed? c int a = 7, b = 2; double x = a / b; double y = (double)a / b; double z = (double)(a / b); int w = (int)(-3.7) +…