2D-array as argument to function

后端 未结 6 881
無奈伤痛
無奈伤痛 2020-11-29 11:00

Why can\'t you declare a 2D array argument in a function as you do with a normal array?

 void F(int bar[]){} //Ok
 void Fo(int bar[][]) //Not ok
 void Foo(in         


        
6条回答
  •  北海茫月
    2020-11-29 11:22

    Since static 2D arrays are like 1D arrays with some sugar to better access data, you have to think about the arithmetic of pointers.
    When the compiler tries to access element array[x][y], it has to calculate the address memory of the element, that is array+x*NUM_COLS+y. So it needs to know the length of a row (how many elements it contains).
    If you need more information I suggest this link.

提交回复
热议问题