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);- A. 6
- B. 3
- C. 0
- 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
- 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); }
- What is printed? (Assume a 64-bit LP64 ABI, where long is 8 bytes.) c long fact(int n) { return n <= 1 ? 1 : n fact(n - 1); } int…
- What is printed? (Assume int is 32 bits.) c int a = -1; unsigned int b = (unsigned int)a; printf("%u", b);