Hello,
why differentiates the compiler in assigning a "pointer to const" an adress directly or via a function call?
void test (const uint16_t *cpui16) { if (*cpui16 == 17) return; } void test1(void) { uint16_t ui16 = 9U; uint16_t *p = &ui16; const uint16_t *cp = &ui16; //warning C102: test(&ui16); //no warning test(p); test(cp); }
And why marks the compiler a warning and the manual an error? Compiler warning C102: '=': different const/volatile qualifiers
Manual *** Error C102
operator: Different const/volatile Qualifiers
I cannnot imagine a potential problem with
const uint16_t *cp = &ui16;
Can anybody give me a hint?
Best regards Jürgen
Thank you for your answer.
I don't understand why "cp" is declared const, but its usage is volatile const and I don't understand the meaning. Can you please give me an explanation or a link to one?
I tried your suggestion but it did not work.
volatile const uint16_t *cp = &ui16;
gives the same error message although I think there is no risk in reading a variable via a const pointer. By the way, all function calls don't produce an error message.
Never mind that, the warning may be compiler specific and it's a bad example anyway; there *is* a difference between "const uint16_t *x" and "volatile const uint16_t *x".
And you do understand what the qualifiers mean, right?
const uint16_t *x; // The value cannot be modified by code nor anything else. volatile const uint16_t *y; // The value cannot be modified by code, // but may be modified by something else, // ie. an interrupt or hardware or another task. uint16_t *x; // The value can be modified by the code and, with the exception // of aliasing, won't be modified by anything else.
Using the qualifiers incorrectly can cause unexpected side effects.
Your example:
uint16_t ui16 = 9U; const uint16_t *cp = &ui16;
issues a warning because you've told the compiler that the value of the referenced address will never change, but you've assigned it an address that *can* change.
For example:
uint16_t ui16 = 9U; uint16_t *p = &ui16; const uint16_t *cp = &ui16; //warning C102: ui16 = 15;
The last statement causes the dereferenced value of "cp" to change, which violates the agreement your code has made with the compiler. That is why you get a warning.
And it deserves a warning because it creates a situation where the program may work as expected, or it may not work as expected, or it may work as expected some of the time.
Oh yes, I think I got the point.
I know the qualifiers 'const' and 'volatile' and I tried to tell the compiler, that I don't want to change the value of the variable ui16 which the pointer cp dereferences via this pointer (as lint suggested).
But what I really did was telling the compiler that pc dereferences a variable that would not change ever. And that is a difference as I see now.
Thanks again for your kind effort although I don't see how I can satisfy lint (which suggested to declare cp as pointing to const) and the compiler (which says the variable cp dereferences isn't really const)