struct構造型態之位元欄位(Bit Fields)成員定義語法:
struct [struct_name]
{
struct_member_declarator
type-specifier [declarator]: constant-expression;
type-specifier [declarator]: constant-expression;
...
struct_member_declarator
}[structVars];
(unnamed field)
(padding -- 30 bits)
toaster
4 bits |
||
|
||
light
|
要存取kitchen 佈局之成員,你可以使用”kitchen.”再加上成員名,如要存取 toaster,語法就是 kitchen.toaster 。 count 1 bit |
|
clock 設置裝備擺設巨細 |
To the next int boundary |
1 bit |
(padding -- 31 bits) |
||
ref: reference IBM : Declaring and Using Bit Fields in Structures 4 bits |
||
1 bit |
||
The size of an int (4 bytes) flag
To the next int boundary (padding -- 23 bits) kitchen成員
此kitchen structure 變數有8個成員,共佔16 bytes 記憶體空間。記憶體設置裝備擺設體式格局如下: |
在C說話的 struct 佈局型態之位元欄(Bit Fields)成員,可定義成各類分歧長度的bit位元型態翻譯例如
To the next int boundary (unnamed field)
#include
#include
struct on_off
{
unsigned light : 1;
unsigned toaster : 1;
int count;/* 4 bytes */
unsigned ac : 4;
//--------------------
unsigned : 4;
unsigned clock : 1;
unsigned : 0;
unsigned flag : 1;
}kitchen;
int main(int argc翻譯社 char *argv[])
{
printf(
"sizeof(kitchen)=%d\r\n" , sizeof(kitchen) );
system("PAUSE");
return 0;
}
ac
1 bit
本文出自: https://blog.xuite.net/yhchiang/blog/151121693-C%E8%AA%9E%E8%A8%80%E7%9A%84struct%E7%B5%90%E6%A7%8B%有關各國語文翻譯公證的問題歡迎諮詢華頓翻譯公司02-77260932