您的位置:首页 > 其它

sizeof

2015-10-23 23:00 260 查看
#include<stdio.h>
int main()
{
struct Test {
int a[3];
char b;
short c;
};
typedef struct Test test2;
struct Test test={{2,4},88,3};
struct Test test1;
printf("%d",sizeof(test2));
printf("%d",sizeof(test));
printf("%d",sizeof(test1));
}

(1) Test只是一个标志,struct Test才是一个类型,因此printf("%d",sizeof(Test))不对。同时sizeof是求的变量或类型。因此3个输出都是16.

(2)对结构体struct Test test={{2,4},88,3};的初始化清楚

由结构想到枚举。

#include<stdio.h>
void main()
{
enum spectrum {red,green,blue,hot,yellow};
enum spectrum color;

printf("%d\n",color);
color=2;
printf("%d\n",color);
color=yellow;
printf("%d\n",color);
color=7;
printf("%d\n",color);
//printf("%d\n",color.red);
printf("%d\n",red);
//printf("%d\n",color.red);
//red=3;
printf("%d\n",red);
printf("%d",sizeof(enum spectrum));
//printf("%d",sizeof(spectrum));
}输出为1 2 4 7 0 0 4

其中的spectrum为标志 ,enum spectrum才是类型,枚举不是结构,不能用color.red. 
enum spectrum {red,green,blue,hot,yellow};中的red green 为常量可以直接输出

谷歌



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: