struct Foo {
int dummy;
} bar;
int main(void)
{
/* This statement causes GCC to produce the warning:
* ISO C forbids casting nonscalar to the same type */
(volatile struct Foo)bar;
/* The warning may be silenced like so.
* Is this form superior or just trickier? Why? */
*(volatile struct Foo *)&bar;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器/标准试图防范什么邪恶?
背景故事:真正的代码是使用ISR和背景共享的循环缓冲区/队列.后台代码不断轮询队列.我希望避免对队列进行访问,同时防止编译器优化队列轮询,以便label: goto label;
在队列为空时代码跳入紧密循环.
警告信息有点误导.ISO C不仅禁止将非标量转换为相同类型; 它禁止向任何类型的任何类型投射非标量.转换运算符的操作数和转换本身中指定的类型都必须是标量类型(算术或指针).
这是一个例外,但它不适用于此.如果目标类型为void,则操作数可以是任何类型.
归档时间: |
|
查看次数: |
271 次 |
最近记录: |