挥发的char *"类型&QUOT的说法;与类型和QUOT的参数不符;为const char *" [英] Argument of type "volatile char *" is incompatible with parameter of type "const char *"

查看:324
本文介绍了挥发的char *"类型&QUOT的说法;与类型和QUOT的参数不符;为const char *"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,其原型如下:

I have a function whose prototype is as follows:

void foo(const char * data);

在别处在我的code,我已经声明为一个全局变量如下

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:

键入挥发性的char *的参数是与类型为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.

感谢

- 阿姆鲁

推荐答案

这是因为隐含的类型转换的添加的预选赛指针类型的目标,而不是将其删除。所以,如果你想你的函数能够接受挥发性和/或常量合格的指针,你必须声明它既:

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 volatile and/or const qualified pointers, you must declare it with both:

void foo(const volatile char * data);

这篇关于挥发的char *"类型&QUOT的说法;与类型和QUOT的参数不符;为const char *"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆