小编Log*_*cG8的帖子

为什么ISO C禁止将非标量转换为相同类型

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;在队列为空时代码跳入紧密循环.

c language-lawyer

3
推荐指数
1
解决办法
271
查看次数

标签 统计

c ×1

language-lawyer ×1