c++ - 函数参数中的C++数组指针

标签 c++ arrays pointers

有人可以向我解释这些功能为何/为什么相同吗?

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/

相关文章:

java - 如何让所有偶数先出现,然后奇数出现?

java - 按单个数组上的 ArrayList 索引(行)对三个数组进行排序

c - 在C中保存一个指针地址

C - 分配内存并将字符串复制到哈希表的数组中

c++ - 如何在qt中包含菜单项的工具提示

javascript - 我如何检查一个数组中的值是否在一个单独的数组中具有特征,如果存在,则在将它们各自转换为新值后返回这些值

c++ - 冲突的不匹配标签与标准库一起编译,但不以其他方式编译

c - 从单链表打印结构数据

c++ - 我如何在 QML 中设置样式表?(只有 QML 没有 C++)

c++ - g++ 错误 : pthread_create() and pointer to member function