Why aren’t these function prototypes equivalent?
void print_matrix(char *name, int SX, int SY, int m[SX][SY])
void print_matrix(char *name, int SX, int SY, int **m)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Even though the two function arguments can be consumed in the same way, namely via
m[i][j], they’re quite different:int m[M][N]is an array ofMarrays ofNints.int **mis a pointer to a pointer to an int.You cannot pass arrays as function arguments, so an “array of
Kelements of typeT” decays to a “pointer-to-T“, pointing to the first element of the array. Thus it is permissible and equivalent to write the first form asint m[][N]in a function argument, since the valueMis lost. However, the valueNis not lost; it is part of the type!So the following are admissible/erroneous for the first form:
For the second form, the meaning is rather different:
The expression
arrdesignates the pointer to the first element of an array of arrays ofNintegers, i.e. its type isint (*)[N]. Dereferencing it gives an array ofNintegers, and not a pointer to an integer.There is no way to convert the expression
arrinto a pointer to a pointer: If you said,then
*foolwould point to the first element of the first array (arr[0]), and not to anintpointer. So you cannot dereference the value further, because the value is not a pointer.The only correct way to pass a two-dimensional array as a double pointer is to construct an intermediate helper array: