相关疑难解决方法(0)

volatile变量作为函数的参数

有这个代码:

typedef volatile int COUNT;       

COUNT functionOne( COUNT *number );

int  functionTwo( int *number );
Run Code Online (Sandbox Code Playgroud)

我无法摆脱一些警告..

我在functionOne原型上得到了这个警告1

函数返回类型时忽略[警告]类型限定符

我得到这个警告2,无论我用一个COUNT 指针参数而不是一个int指针调用functionTwo

[警告]从指针目标类型中抛出限定符

显然变量/指针不能"强制转换"为volatile/un-volatile ..但是每个参数都必须指定为volatile吗?那么如果已经为非易失性变量定义了库函数怎么用呢?

编辑:使用gcc -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wextra -Wstrict-prototypes -Wmissing-prototypes …

编辑:在Jukka Suomela建议之后,这是警告二的代码示例

typedef volatile int COUNT;       

static int functionTwo(int *number) {
    return *number + 1;
}

int main(void) {
    COUNT count= 10;
    count = functionTwo(&count);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c warnings pointers arguments volatile

8
推荐指数
2
解决办法
2万
查看次数

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

使用 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 …

c gcc c99 suppress-warnings

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×2

arguments ×1

c99 ×1

gcc ×1

pointers ×1

suppress-warnings ×1

volatile ×1

warnings ×1