如何消除“丢弃限定符”警告?

ysa*_*sap 5 c gcc c99 suppress-warnings

使用 GCC 和 C99 模式,我有一个函数声明为:

void func(float *X);
Run Code Online (Sandbox Code Playgroud)

当我调用该函数时,我使用了一个可变数组 Y:

volatile float Y[2];
int main()
{
    func(Y);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

When compiling (with -Wall), I get the following warning:

warning: passing argument 1 of ‘func’ discards qualifiers from pointer target type
blah.c:4: note: expected ‘float *’ but argument is of type ‘volatile float *’
Run Code Online (Sandbox Code Playgroud)

I can eliminate it with an explicit (float *) type cast, but this repeats in many places in the code.

Is there a way to eliminate this specific warning, with an option or a pragma (or something equivalent)?

Jon*_*ely 4

不,您无法关闭该警告。它告诉你你违反了类型系统。如果您想调用,func则需要向其传递指向非易失性数据的指针,或者更改函数签名以接受指向易失性数据的指针。

  • 谢谢。这实际上很有趣(在某种程度上也是有争议的)。我并没有真正改变变量的*类型*。“易失性”限定符是优化器的一个信号。所以我想知道为什么它被视为违反类型系统?! (2认同)
  • @rubenvb,乔纳森 - 在接受您的评论时,我确实看到了“const”和“易失性”限定符之间的区别。按照我的理解,“const”限定符提供有关对象的*编译时间*(而不是*优化时间*)信息。例如,它应该阻止我为该对象分配值,如果我尝试这样做,我应该得到诊断。对于 挥发物,就不存在这样的问题(我现在可以想象)。 (2认同)