ProgrammingHardTCS NQT · Infosys pattern
What is printed (64-bit, 4-byte int)?
What is printed (64-bit, 4-byte int)?
int a[5] = {1,2,3,4,5};
int *p = a + 5;
printf("%d", *(p - 1) + *(a + 0));- A. 6
- B. Undefined behavior: p points one past the end, dereferencing it is illegal
- C. 5
- D. 10
Correct answer: A. 6
a + 5 is a valid one-past-the-end pointer (legal to compute, just not to dereference directly). p - 1 brings it back to &a[4] (value 5), which IS valid to dereference. a[0] is 1. 5 + 1 = 6.
More C Programming questions
- What is printed? c int x = 100; void f(int p) { int y = 200; p = &y; } int main() { int p = &x; f(p); printf("%d", p); }
- What is printed? c int arr[3] = {1, 2, 3}; int (p)[3] = &arr; printf("%d %d", (p)[1], (p + 2));
- What is printed (64-bit LP64)? c int a = 10; void vp = &a; int ip = (int )vp; char cp = (char )&a; printf("%d %zu %zu", ip, sizeof(vp)…
- What is printed? c int a = 5; int p = &a; int q = p; p = NULL; printf("%d %d", q, p == NULL);
- What is printed? c int a[2][3] = {{1,2,3},{4,5,6}}; int p = &a[0][0]; printf("%d ", (p + 4)); int (rows)[3] = a; printf("%d", ((rows +…
- What is printed? c int global = 100; int getGlobal() { return &global; } int main() { int p = getGlobal(); p = 200; printf("%d %d"…