2D-array as argument to function

后端 未结 6 894
無奈伤痛
無奈伤痛 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:08

    You can't write void Foo(int bar[][]), because bar decays to a pointer. Imagine following code:

    void Foo(int bar[][]) // pseudocode
    {
        bar++; // compiler can't know by how much increase the pointer
        // as it doesn't know size of *bar
    }
    

    So, compiler must know size of *bar, therefore size of rightmost array must be provided.

提交回复
热议问题