conversion to void * in C and C++

  • Thread starter Thread starter alessandrot60
  • Start date Start date
A

alessandrot60

Guest
If I compile the following (with warning level 4) in a .cpp file,

{
const int ***pV1 = 0;
void *pV2 = pV1;
}

the C++ compiler does not complain, but if I put the same code in a .C file, the C compiler issues the following warning:

warning C4090: 'initializing': different 'const' qualifiers

I don't understand why the presence of "const" at that depth should affect the conversion to void*. I think the behavior of the C++ compiler is correct, but the behavior of the C compiler is puzzling.

Now, if I add a "const" immediately before the last asterisk,

{
const int **const*pV1 = 0;
void *pV2 = pV1;
}


I get an error from the C++ compiler:

error C2440: 'initializing': cannot convert from 'const int **const *' to 'void *'

which is exactly what I would expect.

In other words, my understanding is that the only "const" that should matter in this case is the one that is (or is not) present immediately before the last asterisk. The "const" at the beginning of the first declaration (followed by three asterisks) should be irrelevant. My variable pV2 is a pointer to non-const void. I should be able to convert any pointer to a non-const "thing" to a pointer to non-const void. My variable pV1 is a pointer to non-const pointer to non-const pointer to ... const int, so it *is* a pointer to a non-const "thing".

Is this a bug in the C compiler, or am I missing some rule that is specific to C?

Continue reading...
 
Back
Top