ProgrammingHardIBM · Deloitte pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Point { int x, y; };
struct Point make(int a, int b) {
struct Point p = {a, b};
return p;
}
int main(void) {
struct Point result = make(3, 4);
printf("%d %d", result.x, result.y);
}- A. 3 4
- B. Undefined behaviour: p is local to make() and returning it by value dangles
- C. 0 0
- D. Compile-time error: cannot return a struct by value
Correct answer: A. 3 4
Unlike returning the ADDRESS of a local variable (which would dangle), returning a struct BY VALUE copies its entire contents out before make()'s local p is destroyed; the caller's result gets its own independent copy with the same values, 3 and 4. This is a well-defined, common, and safe idiom in C.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c typedef struct { int x; int y; } Point; Point p1 = {1, 2}; Point p2 = {1, 2}; printf("%d"…
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int value; struct Node next; }; struct Node a = {1, NULL}; struct Node b = {2…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point origin = {0, 0}; struct Point p1 = &origin; struct…
- Assume a 64-bit LP64 ABI. What is printed? c struct Config { int verbose : 1; int debug : 1; int level : 4; }; struct Config cfg = {0}…
- What is printed? c int sumDigits(int n) { if (n == 0) return 0; return (n % 10) + sumDigits(n / 10); } int main(void) { printf("%d"…
- What is printed? c int countDown(int n) { if (n < 0) return 0; printf("%d ", n); return countDown(n - 1); } int main(void) { countDown(3); }