Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;*vs[]作为函数参数_C++_Arrays_Pointers_Parameters_Struct - Fatal编程技术网

C++ C++;*vs[]作为函数参数

C++ C++;*vs[]作为函数参数,c++,arrays,pointers,parameters,struct,C++,Arrays,Pointers,Parameters,Struct,以下两者之间的区别是什么: void foo(item* list) { cout << list[xxx].string; } 指针指向字符数组的第一个字符 而列表只是一个项目数组…它们是相同的-完全同义。第二个是项目列表[],而不是项目列表[] 但是,当参数像数组一样使用时,通常使用[];当它像指针一样使用时,通常使用*。对于编译器来说,没有区别 不过读起来不一样[]表示要向函数传递一个数组,而*也可以表示一个简单的指针 请注意,数组作为参数传递时会衰减为指针(如果您还

以下两者之间的区别是什么:

void foo(item* list)
{
    cout << list[xxx].string;
}
指针指向字符数组的第一个字符


列表
只是一个项目数组…

它们是相同的-完全同义。第二个是
项目列表[]
,而不是
项目列表[]


但是,当参数像数组一样使用时,通常使用
[]
;当它像指针一样使用时,通常使用
*

对于编译器来说,没有区别

不过读起来不一样
[]
表示要向函数传递一个数组,而
*
也可以表示一个简单的指针

请注意,数组作为参数传递时会衰减为指针(如果您还不知道的话)。

FYI:

void foo(int (&a)[5]) // only arrays of 5 int's are allowed
{
}

int main()
{
  int arr[5];
  foo(arr);   // OK

  int arr6[6];
  foo(arr6); // compile error
}

但是
foo(int*arr)
foo(int-arr[])
foo(int-arr[100])
对于“注意数组作为参数传递时会衰减为指针”来说都是等价的。我不知道这一点,这很有趣。不仅数组表达式会衰减为指针,当您将数组类型指定为函数参数时,它实际上会在所有情况下更改为指针类型,包括重载、
sizeof
decltype
。您甚至可以以一种方式声明它,并以另一种方式定义它(但您可能不应该这样做)。最好添加标准中的文本,以免给人留下数组和指针大体相同的印象。函数参数只是一种特殊情况,其中标准规定“在确定每个参数的类型后,将任何类型为“T的数组”[…]的参数调整为“指向T”[…]的指针”[dcl.fct]8.3.5/5。
struct item
{
    char* string;
}
void foo(int (&a)[5]) // only arrays of 5 int's are allowed
{
}

int main()
{
  int arr[5];
  foo(arr);   // OK

  int arr6[6];
  foo(arr6); // compile error
}