C语言结构体数组定义

烟火中的一粒尘埃 2017-09-18 12:05:49
今天无意中看到如下代码:
struct
{
uint8_t column: 5;
uint8_t row: 3;
} key[20];
琢磨了半天,没看懂column: 5,row: 3是啥意思,以前从没见这种结构体数组,还请大神指点一下!
...全文
725 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jobszheng5 2017-10-05
  • 打赏
  • 举报
回复
这个就是C语言的位段定义啊! 在C语言教程里,在数据结构那段里呢
YapingXin 2017-10-05
  • 打赏
  • 举报
回复
这个定义是位域的定义。
destory27 2017-10-05
  • 打赏
  • 举报
回复
位域 : 为了节省内存 不如int 占4个字节 32位 ,int(1), 00000000 00000000 00000000 00000001 ,只占最后一位,,其他高位全是0 ,我们可以把高位的空间分配给其它量
das白 2017-09-18
  • 打赏
  • 举报
回复
后面是位数 占几位....
  • 打赏
  • 举报
回复
位域,就是把一个字节分(8位)成几个块,每块占几位。
赵4老师 2017-09-18
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#pragma pack(push,1)
union U {
    unsigned char byte;
    struct BF {
        unsigned int b0:1;//a
        unsigned int b1:1;//b
        unsigned int b2:1;//c
    } bf;
} u;
#pragma pack(pop)
unsigned char bt;
int a,b,c;
int main() {
    for (bt=0;bt<8;bt++) {
        u.byte=(unsigned char)bt;
        a=u.bf.b0;
        b=u.bf.b1;
        c=u.bf.b2;
        printf("byte 0x%02x -- c:%d b:%d a:%d\n",bt,c,b,a);
    }
    for (c=0;c<2;c++)
    for (b=0;b<2;b++)
    for (a=0;a<2;a++) {
        u.bf.b0=a;
        u.bf.b1=b;
        u.bf.b2=c;
        bt=u.byte;
        printf("c:%d b:%d a:%d -- byte 0x%02x\n",c,b,a,bt);
    }
    return 0;
}
//byte 0x00 -- c:0 b:0 a:0
//byte 0x01 -- c:0 b:0 a:1
//byte 0x02 -- c:0 b:1 a:0
//byte 0x03 -- c:0 b:1 a:1
//byte 0x04 -- c:1 b:0 a:0
//byte 0x05 -- c:1 b:0 a:1
//byte 0x06 -- c:1 b:1 a:0
//byte 0x07 -- c:1 b:1 a:1
//c:0 b:0 a:0 -- byte 0x00
//c:0 b:0 a:1 -- byte 0x01
//c:0 b:1 a:0 -- byte 0x02
//c:0 b:1 a:1 -- byte 0x03
//c:1 b:0 a:0 -- byte 0x04
//c:1 b:0 a:1 -- byte 0x05
//c:1 b:1 a:0 -- byte 0x06
//c:1 b:1 a:1 -- byte 0x07

C Bit Fields In addition to declarators for members of a structure or union, a structure declarator can also be a specified number of bits, called a “bit field.” Its length is set off from the declarator for the field name by a colon. A bit field is interpreted as an integral type. Syntax struct-declarator : declarator type-specifier declarator opt : constant-expression The constant-expression specifies the width of the field in bits. The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value. If the value is zero, the declaration has no declarator. Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components. Unnamed bit fields cannot be referenced, and their contents at run time are unpredictable. They can be used as “dummy” fields, for alignment purposes. An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the struct-declaration-list begins on an int boundary. Bit fields must also be long enough to contain the bit pattern. For example, these two statements are not legal: short a:17; /* Illegal! */ int long y:33; /* Illegal! */ This example defines a two-dimensional array of structures named screen. struct { unsigned short icon : 8; unsigned short color : 4; unsigned short underline : 1; unsigned short blink : 1; } screen[25][80]; The array contains 2,000 elements. Each element is an individual structure containing four bit-field members: icon, color, underline, and blink. The size of each structure is two bytes. Bit fields have the same semantics as the integer type. This means a bit field is used in expressions in exactly the same way as a variable of the same base type would be used, regardless of how many bits are in the bit field. Microsoft Specific —> Bit fields defined as int are treated as signed. A Microsoft extension to the ANSI C standard allows char and long types (both signed and unsigned) for bit fields. Unnamed bit fields with base type long, short, or char (signed or unsigned) force alignment to a boundary appropriate to the base type. Bit fields are allocated within an integer from least-significant to most-significant bit. In the following code struct mybitfields { unsigned short a : 4; unsigned short b : 5; unsigned short c : 7; } test; void main( void ); { test.a = 2; test.b = 31; test.c = 0; } the bits would be arranged as follows: 00000001 11110010 cccccccb bbbbaaaa Since the 8086 family of processors stores the low byte of integer values before the high byte, the integer 0x01F2 above would be stored in physical memory as 0xF2 followed by 0x01. END Microsoft Specific
自信男孩 2017-09-18
  • 打赏
  • 举报
回复
一个字节是8位,column占5位,row占3位。两个字段总共占8位,即一个字节。
kugeniha 2017-09-18
  • 打赏
  • 举报
回复
位域 。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧