General rules of passing/returning reference of array (not pointer) to/from a function?

后端 未结 6 1246
猫巷女王i
猫巷女王i 2020-12-02 06:58

We can pass reference of an array to a function like:

void f(int (&a)[5]);

int x[5];
f(x);     //okay
int y[6];
f(y);     //error - type of y is not `in         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 07:14

    This is tagged C++, so I'm going to suggest that the way to return an array in C++ is to return a std::vector and not try any trickery with C-arrays (which should be used only in carefully selected scenarios in C++ code).

    As other answers noted, you can't return C-arrays from functions.

提交回复
热议问题