Arguments of type ‘volatile char *’ are incompatible with arguments of type ‘const char *’

I have a function whose prototype is as follows:

void foo(const char * data);

Elsewhere in my code, I have a global variable declared as follows

volatile char var[100];

Whenever I try to do this:

foo(var);

The compiler throws the following error message:

Arguments of type 'volatile char *' are incompatible with arguments of type 'const char *'

Why is that? As far as I know, variables in my function are not allowed to change pointers or their contents I understand that because my global variable is changeable, it may change at any time, but it is completely legal to have a changeable const variable. I don't understand why I get this compilation error

thank you

–Amr

Solution

This is because implicit conversions can add qualifiers to the targets of pointer types, but cannot delete them Therefore, if you want a function to accept volatile and / or const qualified pointers, you must declare it using both of the following:

void foo(const volatile char * data);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>