How can I pass a dynamic multidimensional array to a function?

后端 未结 9 2326
再見小時候
再見小時候 2020-12-19 05:48

How can I pass a multidimensional array to a function in C/C++ ?

The dimensions of array are not known at compile time

9条回答
  •  情书的邮戳
    2020-12-19 06:12

    You could pass a pointer and sizes, or use a std::vector. But the "real" solution is with a template:

    template 
    void foo(int (&pArray)[N][M]);
    

    This function template accepts a N by M array of ints, by reference. Note this is a function template, not a function, so you do get a different instantiated function per array type.

提交回复
热议问题