Intel C ++ 162 compiler warning when non-const argument is passed as const parameter

I have a large code base that has recently migrated from the Microsoft compiler to the Intel C ++ compiler. Our team goal is compilation without warnings in the trunk. Since the switch, one warning case 167 confused me. If I compile the following code:

int foo(const int pp_stuff[2][2]) { return 0; } int foo2(const int pp_stuff[][2]) { return 0; } int main(void) { int stuff[2][2] = {{1,2},{3,4}}; foo(stuff); foo2(stuff); return 0; } 

ICC will give me warnings:

 1>main.c(17): warning #167: argument of type "int (*)[2]" is incompatible with parameter of type "const int (*)[2]" 1> foo(stuff); 1> ^ 1> 1>main.c(18): warning #167: argument of type "int (*)[2]" is incompatible with parameter of type "const int (*)[2]" 1> foo2(stuff); 

Why should this be a warning? It is usually customary to pass a non-constant variable as a const parameter, and the types and sizes are identical.

For those who noted this duplicate question, I urge you to reconsider. If anyone else comes across this warning, they should be aware that they are converted in C arguments, as if they were assigned in prototyped functions, and then looked for a question that is strictly related to assignment. Although the answer ends with the same sentence from C90 / C99, the question, I think, is very different.

+4
source share
2 answers

Pass your variable as const when you pass it to a function that requires const.

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

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

Similar question

+2
source

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

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

equivalently

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

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

Arguments are converted as if they were assigned in prototyped functions. And C allows you to assign two pointers if:

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

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

Using:

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

if you want to make a const pointer, not an int .

+2
source

All Articles