ProgrammingHardGATE CS · Campus placements
void f(int *a) { size_t n = sizeof a / sizeof a[0]; ... } is called as f(arr) where int arr[4]. What is true about n?
- A. It equals 4 for any array passed in
- B. It equals sizeof(int *) / sizeof(int), which is not the length of arr
- C. It is a compile-time error
- D. It is always 0
Correct answer: B. It equals sizeof(int *) / sizeof(int), which is not the length of arr
The parameter is a pointer, so sizeof a is the pointer size and the division measures the pointer, not the array. The length must be passed as a separate argument.
More C Programming questions
- unsigned int n = 0u; int i = 0; if (i < n - 1) printf("loop"); What is printed?
- Is the C standard guaranteed to remove stack frames of tail-recursive calls, so that deep tail recursion cannot overflow the stack?
- int a[6] = {1, 2, 3, 4, 5, 6}; int p = a, q = a + 5; while (p < q) { int t = p; p++ = q; q-- = t; } What is a[1] afterwards?
- uint8t x = 0xB4; What is the lowest set bit of x, computed as x & (uint8t)-x?
- uint8t rev(uint8t x) { uint8t r = 0; for (int i = 0; i < 8; i++) { r = (uint8t)((r << 1) | (x & 1)); x >>= 1; } return r; } What is…
- int a[] = {4, 9, 16}; int p = a; What is (p + 2)[-1]?