的C结构的大小 [英] size of C structure

查看:98
本文介绍了的C结构的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct st1{
    int a:1; int b:3; int c:6; int d:3;
}s1;

struct st2{
    char a:3;
}s2;

int main(){
    printf("%d : %d",sizeof(s1),sizeof(s2));
    getchar();
}    

我收到输出为2:1。

I am getting the output as 2 : 1

您会请告诉我,这个方案是如何工作的和最新的使用:运算符(1)。在这里

will you please tell me, how this program works and whats the use of : operator (a:1) here.

感谢您

推荐答案

定义的位域

在你的榜样,类型的对象结构ST1 在编译器中选择的一些安排使用13位。

In your example, objects of type struct st1 use 13 bits in some arrangement chosen by the compiler.

在编译code起源占用2个字节的对象选择的具体安排。的13位不一定是那些字节的第一(或最后)。

The particular arrangement chosen when you compiled the code originated an object that occupies 2 bytes. The 13 bits are not necessarily the first (or last) in those bytes.

其他的结构类型(结构ST2 )占用(3位出),1个字节。

The other struct type (struct st2) occupies (3 bits out of) 1 byte.

这篇关于的C结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆