Intel C++ Compiler warning 167 when non-const argument is passed as const parameter [duplicate]

怎甘沉沦 提交于 2019-12-04 09:29:36

Cast your variable as const when you pass it to the function that requires const.

foo( (const int (*)[2]) stuff );

Why can't I pass a char ** to a function which expects a const char **?

Similar question

The value of stuff array is of type int (*)[2].

int foo(const int pp_stuff[2][2])

is equivalent to

int foo(const int (*pp_stuff)[2])

In the function call, it is as if you were assigning a value of type int (*)[2] to a variable of type const int (*)[2].

Arguments are converted as if by assignment in prototyped functions. And C let you assign two pointers if:

both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

Here int (*)[2] and const int (*)[2] are not qualified/unqualified versions of the same type. The qualifier applies to int not to the pointer.

Use:

int foo(int (* const pp_stuff)[2])

if you want to make the pointer const and not the int elements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!