ProgrammingHardHCL · Tech Mahindra pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int x = 5;
int *p = &x;
int **pp = &p;
*pp = NULL;
printf("%d", p == NULL);- A. 0
- B. 1
- C. Undefined behaviour: assigning NULL through a pointer-to-pointer
- D. Compile-time error: cannot assign NULL to *pp
Correct answer: B. 1
*pp dereferences pp to reach p itself (since pp holds &p), so *pp = NULL; actually reassigns the pointer variable p in the caller's scope to NULL — this is exactly the mechanism used to let a function modify a caller's own pointer. p == NULL is then true, printing 1.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; int q = p; p = NULL; printf("%d %d", p == NULL, q);
- Assume a 64-bit LP64 ABI. What is printed? c int rows = 2, cols = 3; int matrix = malloc(rows sizeof(int )); for (int i = 0; i < rows…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 1; int p = &x; int pp = &p; int ppp = &pp; printf("%d", ppp == &pp && ppp == pp &&…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {10, 20, 30}; int b[3] = {10, 20, 30}; printf("%d %d", a == b, a == b);
- Assume a 64-bit LP64 ABI. What is printed? c int globalArr[5] = {1, 2, 3, 4, 5}; void resetArr(void) { for (int i = 0; i < 5; i++)…
- What is printed? c int main(void) { char c = 200; printf("%d", c); }