ProgrammingHardHCL · Tech Mahindra pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Point { int x, y; };
void move(struct Point *p, int dx, int dy) {
p->x += dx;
p->y += dy;
}
int main(void) {
struct Point pt = {1, 2};
move(&pt, 10, 20);
printf("%d %d", pt.x, pt.y);
}- A. 1 2
- B. 11 22
- C. Compile-time error: -> cannot be used on a struct pointer parameter
- D. Undefined behaviour: pt is passed by reference unintentionally
Correct answer: B. 11 22
&pt passes the address of the actual struct, and p->x is shorthand for (*p).x, correctly reaching and modifying the caller's real pt through the pointer. After move(), pt.x = 1+10=11 and pt.y = 2+20=22, and these changes are visible in main because a pointer (not a copy of the struct) was passed.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c void modify(int x) { x = 100; } void modifyPtr(int x) { x = 100; } int main(void) { int a =…
- Assume a 64-bit LP64 ABI. What is printed? c int allocateArray(int n) { int arr = malloc(n sizeof(int)); for (int i = 0; i < n; i++)…
- Assume a 64-bit LP64 ABI. What is printed? c int compare(const void a, const void b) { return (const int )a - (const int )b; } int…
- Assume a 64-bit LP64 ABI. What is printed? c void fillWithPointer(int pp, int value) { pp = malloc(sizeof(int)); pp = value; } int…
- Assume a 64-bit LP64 ABI. What is printed? c char str[] = "hello"; char p = str; while (p != '0') { p = p - 32; p++; } printf("%s", str);
- Assume a 64-bit LP64 ABI. What happens when this runs? c char str = "hello"; str[0] = 'H'; printf("%s", str);