Interview Question
Country: United States
error in code
type mismatch . from calling to receiving .
in f change
int (*c)[2]
output : 1
I guess you might get that too, because the array in the caller method is only pretending to be a 2-D array when it's a 1-D array.
I'm saying that the declaration int c[2][2]={1,2,3,4}; makes a 1-D array and not an array of arrays (2-D array). One of the things I think is so very wrong with C and C++
Let me describe you why?
int c[2][2] ; is actually like that int (*c)[2] and c = new int [2][2];
but int **c ; is different from that .....
that's y
in case of 1D array
int c[2] ; its actually like that int *c = new int [2] ; which is similir with int *c ;
i have use dynamic array for clarifying my point only
2-D arrays allocated on the stack are actually allocated as a contiguous 1-D array. When you pass this to a method, methods have no idea what something like array[X][Y] refers to because it needs to refer to array[X*ySize+Y], but the called method has no size information. In my opinion, this is a very unfortunate feature of C. 1-D arrays shouldn't be masquerading as 2-D arrays.
- eugene.yarovoi June 28, 2012