ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
struct Node {
    int value;
    struct Node *next;
};
struct Node a = {1, NULL};
struct Node b = {2, NULL};
struct Node c = {3, NULL};
a.next = &b;
b.next = &c;
int sum = 0;
for (struct Node *p = &a; p != NULL; p = p->next) {
    sum += p->value;
}
printf("%d", sum);
  1. A. 6
  2. B. 3
  3. C. 0
  4. D. Compile-time error: struct Node cannot contain a pointer to itself

Correct answer: A. 6

A struct may contain a pointer to its own type (this is how linked lists work), since a pointer's size doesn't depend on the pointee's completeness. The list a->b->c is manually linked, and walking it with p->next, summing values, adds 1+2+3 = 6.

More C Programming questions

Exams that ask this topic