I have a question about how C / C++ internally stores multidimensional arrays declared using the notation foo[m][n]
. I am not questioning pure pointers to pointers etc... I am asking because of speed reasons...
Correct me if I am wrong, but syntactically foo
is an array of pointers, which themselves point to an array
int foo[5][4]
*(foo + i) // returns a memory address
*( *(foo + i) + j) // returns an int
I have heard from many places that the C/C++ compiler converts foo[m][n]
to a one dimensional array behind the scenes (calculating the required one dimension index with i * width + j
). However if this was true then the following would hold
*(foo + 1) // should return element foo[0][1]
Thus my question:
Is it true that foo[m][n]
is (always?) stored in memory as a flat one dimensional array?? If so, why does the above code work as shown.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…