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 up the following error message:
Argument of type “volatile char *” is incompatible with parameter of type “const char *”
Why is that the case? As I understand it, the variable in my function is not allowed to change the pointer or its contents. I understand that because my global variable is volatile, it could potentially change at any time, but seeing as it is perfectly legal to have a volatile const variable, I don’t see why I am getting this compiler error.
Thanks
–Amr
It’s because implicit conversions can add qualifiers to the target of pointer types, but not remove them. So if you want your function to be able to accept
volatileand/orconstqualified pointers, you must declare it with both: