ProgrammingHardGATE CS · Product company OA
Given int m[3][4];, why can m not be passed to a function declared void f(int **p);?
- A. m is too large to pass by value
- B. A two-dimensional array cannot be passed to any function
- C. int ** can only point to dynamically allocated memory
- D. m converts to int (*)[4], a pointer to a row of 4 ints, which is a different type with a different layout from a pointer to pointers
Correct answer: D. m converts to int (*)[4], a pointer to a row of 4 ints, which is a different type with a different layout from a pointer to pointers
m is one contiguous block of 12 ints, so it decays to a pointer to its first row. f would read the ints as if they were row pointers. Declare f(int (*p)[4]) or f(int p[][4]) instead.
More C Programming questions
- Assume C99 or later. What is printed? c void f(int n, int a[][n]) { printf("%zu", sizeof a[0] / sizeof a[0][0]); } int main(void) { int…
- What is printed? c int c = 0; for (int i = 1; i <= 10; i++) for (int j = i; i j <= 10; j++) c++; printf("%d", c);
- What does z("abcdef") print? c void z(const char s) { if (!s) return; putchar(s); z(s + 2); if (s[1]) putchar(s[1]); }
- Does this function compile, and what does it do? c struct S { int p; }; void f(const struct S s) { s->p = 5; }
- On a two's-complement machine, what is printed? c int x = 5; printf("%d %d", -~x, ~-x);
- What is printed? c const char s = "ab cd"; printf("%zu", strlen(s));