C中结构体可以定义位域吗?如何定义?
来源:百度知道 编辑:UC知道 时间:2024/09/24 22:28:41
#include <iostream>
#include "stdio.h"
using namespace std;
struct s1
{
int i:8;
int j:4;
double b;
int a:3;
};
struct s2
{
int i;
int j;
double b;
int a;
};
struct s3
{
int i;
int j;
int a;
double b;
};
struct s4
{
int i:8;
int j:4;
int a:3;
double b;
};
int main()
{
cout<<sizeof(s1)<<endl; // 24
cout<<sizeof(s2)<<endl; // 24
cout<<sizeof(s3)<<endl; // 24
cout<<sizeof(s4)<<endl; // 16
return 0;
}
该段程序提示出错,为什么?
#include "stdio.h"
using namespace std;
struct s1
{
int i:8;
int j:4;
double b;
int a:3;
};
struct s2
{
int i;
int j;
double b;
int a;
};
struct s3
{
int i;
int j;
int a;
double b;
};
struct s4
{
int i:8;
int j:4;
int a:3;
double b;
};
int main()
{
cout<<sizeof(s1)<<endl; // 24
cout<<sizeof(s2)<<endl; // 24
cout<<sizeof(s3)<<endl; // 24
cout<<sizeof(s4)<<endl; // 16
return 0;
}
该段程序提示出错,为什么?
/* Win-TC 编译器 */
#include <stdio.h>
struct s1
{
int i:8;
int j:4;
double b;
int a:3;
};
struct s2
{
int i;
int j;
double b;
int a;
};
struct s3
{
int i;
int j;
int a;
double b;
};
struct s4
{
int i:8;
int j:4;
int a:3;
double b;
};
int main(void)
{
printf("%d\n",sizeof(struct s1)); /* 11 */
printf("%d\n",sizeof(struct s2)); /* 14 */
printf("%d\n",sizeof(struct s3)); /* 14 */
printf("%d\n",sizeof(struct s4)); /* 10 */
return 0;
}
LZ写的好像不是C程序吧?