ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
typedef struct {
int code;
char message[20];
} Error;
Error makeError(int c, const char *msg) {
Error e;
e.code = c;
strncpy(e.message, msg, 19);
e.message[19] = '\0';
return e;
}
int main(void) {
Error e = makeError(404, "Not Found");
printf("%d %s", e.code, e.message);
}- A. 0
- B. 404
- C. Compilation error: cannot return a local struct containing an array
- D. 404 Not Found
Correct answer: D. 404 Not Found
Returning a struct by value (even one containing a fixed-size array member) copies the ENTIRE struct's bytes out of the function; there's no dangling-pointer issue here since nothing is returned by reference. e.code=404 and e.message="Not Found" are both correctly copied out.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct Counter { int value; }; void increment(struct Counter c) { c->value++; } int…
- Assume a 64-bit LP64 ABI. What is printed? c struct Pair { int a, b; }; struct Pair swapPair(struct Pair p) { int tmp = p.a; p.a = p.b…
- Assume a 64-bit LP64 ABI. What is printed? c struct Employee { char name[20]; int id; float salary; }; struct Employee e1 = {"Alice", 101…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point make(int a, int b) { struct Point p = {a, b}; return…
- 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…