ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int a = 5;
int *p = &a;
int *q = p;
p = NULL;
printf("%d %d", *q, p == NULL);- A. 5 1
- B. 0 1
- C. Undefined behavior: cannot dereference NULL
- D. 5 0
Correct answer: A. 5 1
q was assigned a COPY of p's original value (the address of a) before p was set to NULL, so q is unaffected and still validly points at a; *q is 5. p itself is now NULL, so p == NULL is true (1).
More C Programming questions
- 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"…
- What is printed? c void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; int p = &x, q = &y; swap(&p…
- Assume 64-bit LP64, natural alignment. What is printed? c struct A { int i; char c; }; struct B { char c; int i; }; printf("%zu %zu"…
- Assume 64-bit LP64, natural alignment. What is printed? c struct S { double d; int i; }; struct S arr[3]; printf("%zu %zu", sizeof(struct…
- Assume 64-bit LP64. What is printed? c struct Node { int val; struct Node next; }; printf("%zu", sizeof(struct Node));