ProgrammingHardHCL · Tech Mahindra pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
void fillWithPointer(int **pp, int value) {
*pp = malloc(sizeof(int));
**pp = value;
}
int main(void) {
int *p = NULL;
fillWithPointer(&p, 42);
printf("%d", *p);
free(p);
}- A. 0
- B. Undefined behaviour: p is never actually changed in main
- C. 42
- D. NULL pointer dereference
Correct answer: C. 42
Passing &p (a pointer to the pointer p) lets fillWithPointer actually reassign p itself in main, not just a local copy of it. *pp = malloc(...) stores the new heap address into p (through the pointer-to-pointer), and **pp = value writes 42 into that freshly allocated int. Back in main, p now correctly points at that allocated int holding 42.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c char str[] = "hello"; char p = str; while (p != '0') { p = p - 32; p++; } printf("%s", str);
- Assume a 64-bit LP64 ABI. What happens when this runs? c char str = "hello"; str[0] = 'H'; printf("%s", str);
- Assume a 64-bit LP64 ABI. What is printed? c struct Flags { unsigned isActive : 1; unsigned isAdmin : 1; unsigned level : 6; }; struct…
- Assume a 64-bit LP64 ABI. What is printed? c union Data { int i; char c; }; union Data d; d.i = 0x12345678; printf("%x", (unsigned…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void tryModify(struct Point p) { p.x = 999; } int main(void) {…
- Assume a 64-bit LP64 ABI. What is printed? c struct Rect { struct { int x, y; } topLeft; struct { int x, y; } bottomRight; }; struct Rect…