小编ipl*_*ast的帖子

为什么sizeof给出了错误的答案

好吧,我遇到了一个古怪,也许有人可以解释它.源代码是(c ++ 11):

?#?include? <stdio.h>
struct xyz_ {
    float xyz[3];
    float &x = xyz[0];
    float &y = xyz[1];
    float &z = xyz[2];
};

int main(int argc, char *argv[])
{
    xyz_ xyz;
    xyz.x = 0;
    xyz.y = 1;
    xyz.z = 2;
    xyz.xyz[1] = 1;
    printf("as array %f %f %f\n",xyz.xyz[0],xyz.xyz[1],xyz.xyz[2]);
    printf("as elements %f %f %f\n",xyz.x,xyz.y,xyz.z);
    int sizexyz = sizeof(xyz);
    int sizefloat = sizeof(float);
    printf("float is %d big, but xyz is %d big\n",sizefloat,sizexyz);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

as array 0.000000 1.000000 2.000000
as …
Run Code Online (Sandbox Code Playgroud)

c++11

0
推荐指数
1
解决办法
97
查看次数

标签 统计

c++11 ×1