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);
}
  1. A. 3 4
  2. B. Undefined behaviour: p is local to make() and returning it by value dangles
  3. C. 0 0
  4. 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

Exams that ask this topic