ProgrammingVery hardProduct company OA
What is printed, assuming no undefined-behavior crash occurs on this platform?
What is printed, assuming no undefined-behavior crash occurs on this platform?
int a[3] = {1,2,3};
int b[3] = {4,5,6};
int *p = a + 3;
int *q = b;
printf("%d", p == q);- A. 1, since both addresses are the last byte of a's storage
- B. 0, guaranteed by the standard
- C. 1, guaranteed by the standard
- D. Unspecified/implementation-defined (comparing one-past-the-end of a with the start of an unrelated array)
Correct answer: D. Unspecified/implementation-defined (comparing one-past-the-end of a with the start of an unrelated array)
Comparing pointers to two different objects (even one-past-the-end of one) for equality is well-defined only in that it compares addresses, but there is no guarantee a's one-past-end address differs from or equals b's start; the standard permits either outcome depending on memory layout, and C gives no guarantee about relative placement of unrelated arrays. It is not UB to compare them with == (unlike <, > across unrelated objects), but the result is not fixed by the standard.
More C Programming questions
- What is printed? c int x = 5; void vp = &x; int ip = (int)vp; (ip)++; printf("%d", (int)vp);
- What is printed? c int a[5] = {1,2,3,4,5}; int p = a; for (p = a; p < a + 5; p++) { if (p == a + 2) continue; printf("%d", p); }
- What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; int pp = &p; pp += 100; (pp)++; pp += 100; printf("%d…
- What is printed? c int a[6] = {1,2,3,4,5,6}; int p = a + 1; int q = a + 4; int diff = q - p; q -= diff / 3 2; printf("%d %d", diff, q);
- Assume a 64-bit LP64 ABI. What is printed? c int a[3][2] = {{1,2},{3,4},{5,6}}; int (p)[2] = a + 1; int q = (int)p; q++; printf("%d %d"…
- What is printed? c int a[3] = {5,10,15}; int p = a; int q = &a[2]; while (p != q) { printf("%d ", p); p++; } printf("%d", p);