ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
int x = 5;
void *vp = &x;
int *ip = (int*)vp;
(*ip)++;
printf("%d", *(int*)vp);- A. 5
- B. 6
- C. Compilation error: cannot dereference void*
- D. Undefined behavior, value unpredictable
Correct answer: B. 6
vp and ip both hold the address of x. Incrementing through ip modifies x to 6; reading back through a cast void* reads the same memory, so 6 is printed. void* itself cannot be dereferenced directly, but casting it to int* first is legal and exactly what both expressions do.
More C Programming questions
- 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);
- What is printed? c int a[3] = {1,2,3}; int p = a; int arrp[1] = {p}; int pp = arrp; (pp)[1] = 99; printf("%d %d %d", a[0], a[1], a[2]);