ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int global = 10;
int *getPtr(void) {
return &global;
}
int main(void) {
int *p = getPtr();
*p = 20;
printf("%d %d", global, *getPtr());
}- A. 10 10
- B. 20 20
- C. 10 20
- D. 20 10
Correct answer: B. 20 20
global has static storage duration, so its address remains valid for the entire program; returning &global is perfectly safe (unlike returning the address of a local variable). *p = 20 modifies the actual global, and getPtr() called again afterward returns the same still-valid address, reading the now-updated value: 20 20.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void move(struct Point p, int dx, int dy) { p->x += dx; p->y +=…
- 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);