ProgrammingHardProduct-company MCQ pattern
With 4-byte int, consider int (*p)[5]; Which is correct?
- A. p is an array of 5 pointers to int, and p + 1 moves 4 bytes ahead
- B. p is an array of 5 pointers to int, and p + 1 moves 8 bytes ahead
- C. p is a pointer to an array of 5 ints, and p + 1 moves 20 bytes ahead
- D. p is a pointer to an array of 5 ints, and p + 1 moves 4 bytes ahead
Correct answer: C. p is a pointer to an array of 5 ints, and p + 1 moves 20 bytes ahead
The parentheses bind * to p first, so p is a pointer, and what it points to is int[5]. Pointer arithmetic steps by the size of the pointee: 5 × 4 = 20 bytes. int *p[5] (no parentheses) would be an array of 5 pointers.
More C Programming questions
- int a[3][4]; is stored in row-major order starting at address 1000, with 4-byte int. What is the address of a[2][1]?
- What is printed? c unsigned char c = 250; c += 10; printf("%d", c);
- What does f(3) print? c void f(int n) { if (n > 0) { f(n - 1); printf("%d ", n); f(n - 1); } }
- What is printed? c char a[] = "hi", b[] = "hi"; if (a == b) printf("same"); else printf("different");
- What is printed? c #define STR(x) #x #define CAT(a, b) a##b int xy = 7; printf("%s %d", STR(x+y), CAT(x, y));
- What is printed? c int x = 2; switch (x) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); break; default: printf("D"); }