ProgrammingHardAccenture · Cognizant pattern
What is printed (64-bit LP64)?
What is printed (64-bit LP64)?
int a = 10;
void *vp = &a;
int *ip = (int *)vp;
char *cp = (char *)&a;
printf("%d %zu %zu", *ip, sizeof(vp), sizeof(*cp));- A. 10 8 4
- B. 10 4 4
- C. 10 4 1
- D. 10 8 1
Correct answer: D. 10 8 1
sizeof(vp) is the size of a pointer itself (8 bytes on LP64), regardless of what it points to. sizeof(*cp) is sizeof(char) = 1 by definition. *ip correctly reads back the int value 10 since vp was cast through int*.
More C Programming questions
- What is printed? c int a = 5; int p = &a; int q = p; p = NULL; printf("%d %d", q, p == NULL);
- What is printed? c int a[2][3] = {{1,2,3},{4,5,6}}; int p = &a[0][0]; printf("%d ", (p + 4)); int (rows)[3] = a; printf("%d", ((rows +…
- What is printed? c int global = 100; int getGlobal() { return &global; } int main() { int p = getGlobal(); p = 200; printf("%d %d"…
- What is printed? c void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; int p = &x, q = &y; swap(&p…
- Assume 64-bit LP64, natural alignment. What is printed? c struct A { int i; char c; }; struct B { char c; int i; }; printf("%zu %zu"…
- Assume 64-bit LP64, natural alignment. What is printed? c struct S { double d; int i; }; struct S arr[3]; printf("%zu %zu", sizeof(struct…