有人可以向我解释这些功能为何/为什么相同吗?
void doubleArray(int* values, int length)
void doubleArray(int values[], int length)
我不必为使代码仍然完全相同而更改任何内容,但是我不确定逻辑。我希望至少要更改函数中编写的内容。如果需要的话,下面是完整的代码;
#include <iostream>
using std::cout;
using std::endl;
void doubleArray(int* values, int length);
int main() {
int length = 10;
int values[length];
for (int i = 0; i < length; i++){
values[i] = i;
}
doubleArray(values, length);
for (int i = 0; i < length; i++) {
cout << values[i] << endl;
}
}
void doubleArray(int* values, int length) {
for(int i = 0; i < length; i++){
values[i] = values[i] * 2;
}
}
最佳答案
从https://en.cppreference.com/w/cpp/language/function#Parameter_list:
The type of each function parameter in the parameter list is determined according to the following rules:
[..]
2) If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T"
[..]
Because of these rules, the following function declarations declare exactly the same function:
int f(char[]); int f(char* s); int f(char* const); int f(char* volatile s);
关于c++ - 函数参数中的C++数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60810214/