相关疑难解决方法(0)

为什么sizeof(x ++)不增加x?

这是在dev c ++ windows中编译的代码:

#include <stdio.h>

int main() {
    int x = 5;
    printf("%d and ", sizeof(x++)); // note 1
    printf("%d\n", x); // note 2
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

x执行注释1后,我希望是6 .但是,输出是:

4 and 5
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么x注1后没有增加?

c sizeof

494
推荐指数
7
解决办法
3万
查看次数

为什么sizeof(my_arr)[0]编译并且sizeof(my_arr [0])?

为什么这段代码会编译?

_Static uint32_t my_arr[2];
_Static_assert(sizeof(my_arr) == 8, "");
_Static_assert(sizeof(my_arr[0]) == 4, "");
_Static_assert(sizeof(my_arr)[0] == 4, "");
Run Code Online (Sandbox Code Playgroud)

前两个断言显然是正确的,但我本来期望最后一行失败,因为我的理解是sizeof()应该求值为整数文字,不能将其视为数组.换句话说,它将以与以下行失败相同的方式失败:

_Static_assert(4[0] == 4, "");
Run Code Online (Sandbox Code Playgroud)

有趣的是,以下确实无法编译(应该做同样的事情,不是吗?):

_Static_assert(*sizeof(my_arr) == 4, "");
Run Code Online (Sandbox Code Playgroud)

错误:一元'*'的无效类型参数(具有'long unsigned int')_Static_assert(*sizeof(my_arr)== 4,"");

如果重要,我正在使用gcc 5.3.0

c sizeof

127
推荐指数
4
解决办法
6444
查看次数

标签 统计

c ×2

sizeof ×2