ProgrammingHardProduct company OA
With 4-byte int, what is printed?
With 4-byte int, what is printed?
int a[4] = {1, 2, 3, 4};
char *c = (char *)a;
int *p = (int *)(c + 8);
printf("%d %td", *p, p - a);- A. 2 2
- B. 3 2
- C. 9 2
- D. 3 8
Correct answer: B. 3 2
Moving a char pointer by 8 moves 8 bytes, which is two ints, so p points at a[2] = 3. p − a counts ints, giving 2. '8' mixes the byte offset into the int-pointer difference.
More C Programming questions
- What does Staticassert(sizeof(long) >= 8, "need 64-bit long"); do in C11?
- What is printed? c int i = 0, s = 0; do { if (++i == 3) continue; if (i == 5) break; s += i; } while (i < 10); printf("%d %d", s, i);
- What is printed? c struct N { int v; struct N nx; }; void del(struct N h, int x) { while (h) { if ((h)->v == x) h = (h)->nx; else h =…
- What is printed? c int rot(const char a, const char b) { char buf[32]; if (strlen(a) != strlen(b)) return 0; strcpy(buf, a); strcat(buf…
- What is printed? c int alt(unsigned x) { unsigned y = x ^ (x >> 1); return (y & (y + 1)) == 0; } int main(void) { printf("%d%d%d", alt(5)…
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } o = {{{{1, 2}}, {{3, 4}}}, 5}; printf("%d %d"…