Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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++_Pointers_Constants - Fatal编程技术网

C++ 将指针数组作为常量传递

C++ 将指针数组作为常量传递,c++,pointers,constants,C++,Pointers,Constants,作为常量传递的数组与作为常量传递的数组值之间有什么区别 当每个值都是常量时,向函数传递指针数组时: `void display(Fraction* const ar[], int size);` 一切正常,但当数组为常量时 `void display(const Fraction* ar[], int size);` 编译器在调用函数时出现以下错误: `error C2664: 'display' : cannot convert parameter 1 from 'Fraction *

作为常量传递的数组与作为常量传递的数组值之间有什么区别

当每个值都是常量时,向函数传递指针数组时:

`void display(Fraction* const ar[], int size);`
一切正常,但当数组为常量时

`void display(const  Fraction* ar[], int size);` 
编译器在调用函数时出现以下错误:

`error C2664: 'display' : cannot convert parameter 1 from 'Fraction *[3]' to 'const Fraction *[]'`
主要内容:

这是一个很好的例子

请注意,
const T*a[]
表示
T const*a[]
,即您声明的
const
不是数组本身;相反,您声明了一个指向
const
项的指针数组

本质上,如果语言提供了隐式转换
T**
T const**
,则您可能会无意中尝试更改最初声明的
const

int const     v = 666;
int*          p;
int**         pp = &p;
int const**   ppHack = pp;    //! Happily not allowed!

*ppHack = &v;    // Now p points to the const v...
这是一个很好的例子

请注意,
const T*a[]
表示
T const*a[]
,即您声明的
const
不是数组本身;相反,您声明了一个指向
const
项的指针数组

本质上,如果语言提供了隐式转换
T**
T const**
,则您可能会无意中尝试更改最初声明的
const

int const     v = 666;
int*          p;
int**         pp = &p;
int const**   ppHack = pp;    //! Happily not allowed!

*ppHack = &v;    // Now p points to the const v...

请告诉我链接的最新情况好吗?我正在并行地用谷歌搜索,但这会有所帮助。@DavidTóth:完成。常见问题解答现在位于。我可以要求更新链接吗?我正在并行地用谷歌搜索,但这会有所帮助。@DavidTóth:完成。常见问题解答现在位于。我假设您不打算在
display
中修改
ar
,因此您可以在任何地方添加
const
,即
分数常量*const ar[]
,这是一种合法的转换。我假设您不打算在
display
中修改
ar
,因此,您可以在任何地方添加
const
,即
分数const*const ar[]
,这是一种合法的转换。