There is such code:
int tab[14][2];
int (*wskk)[2] = tab; // &tab makes error
int tab2[2];
wskk = &tab2; // tab2 makes error
Why is it possible to use one pointer to point at two arrays of different dimensions?
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.
To understand what’s going on you must be familiar with a few key-concepts:
wskkis “pointer to an array of 2ints”.Thus, if you write
tabyou’re getting a pointer to the first element oftab, which is its first row; the row has typeint[2], so a pointer to it has typeint (*)[2], which is exactly the type of your pointer. Because of this you can assigntabtowskk, which will now point to the first row oftab.You can’t assign
&tabto it, because that yields you a pointer to the whole multidimensional array, which is of typeint (*)[14][2].As for the second piece, it’s even simpler:
tab2is an array of twoints, so its type isint[2]. If you get a pointer to it via the&operator, you get aint (*)[2], which is the type of your pointer. Actually, it makes sense:tab2and a row oftabare effectively the same stuff (an array of 2ints).You can’t assign
tab2to it becausetab2decays to a pointer to its first element, i.e. anint *.