ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Union က struct နဲ့ ပုံစံတူပေမယ့် member တစ်ခုစီကို ကိုယ်ပိုင်နေရာ ပေးမယ့်အစား member အားလုံးကို နေရာ တစ်ခုတည်း ပေးပါတယ်။ ဆိုင်ကယ်၊ ကား ဒါမှမဟုတ် ဗန်ကား တစ်စီးစီ ရပ်လို့ရတဲ့ ကားရပ်နေရာ တစ်ကွက်ကို မြင်ကြည့်ပါ — ကား ရပ်လိုက်ရင် ဆိုင်ကယ် ထွက်သွားရပြီး နေရာရဲ့ အရွယ်အစားကလည်း ဆံ့ရမယ့် အကြီးဆုံး ယာဉ်လောက် ကြီးရပါတယ်။
ဒါကြောင့် union က type စာရင်းထဲက တစ်ခုကို တစ်ကြိမ်မှာ value တစ်ခုပဲ ကိုင်ထားပါတယ်။ Data တစ်ခုက အမျိုးအစား တစ်မျိုး ဒါမှမဟုတ် တခြားတစ်မျိုး ဖြစ်နိုင်ပေမယ့် နှစ်မျိုးလုံး တစ်ပြိုင်တည်း မဖြစ်နိုင်တဲ့အခါ memory သက်သာစေပါတယ် — ဥပမာ spreadsheet ထဲက cell တစ်ကွက်မှာ ဂဏန်း ဒါမှမဟုတ် စာသား တစ်ခုခုပဲ ပါတာမျိုးပါ။
Problem → Why → Solution
ပြဿနာ: union က ဘယ် member ကို ရေးခဲ့လဲ မမှတ်ထား
Value တစ်ခုစီက integer ဒါမှမဟုတ် ဒသမကိန်း ဖြစ်နိုင်တဲ့ settings file တစ်ခုကို စဉ်းစားကြည့်ပါ။ နေရာ သက်သာအောင် value တွေကို union ထဲ သိမ်းပြီး float member ထဲ 220.5 ရေးလိုက်တယ်၊ နောက်ပိုင်း value က integer လို့ ယူဆထားတဲ့ program ရဲ့ တခြား အစိတ်အပိုင်းက int member ကို ဖတ်တယ် ဆိုပါစို့။ သူရတာက အဓိပ္ပာယ်မဲ့ ဂဏန်းကြီး တစ်ခုပါ — သာမန် x86-64 PC မှာ 1130135552 ရပြီး ဘာ warning မှ မပေါ်ပါဘူး။
#include <stdio.h>
union Setting {
int i;
float f;
};
int main(void) {
union Setting volume;
volume.f = 220.5f; /* stored as a float ... */
printf("%d\n", volume.i); /* ... read back as an int: garbage */
return 0;
}ဘာကြောင့် ဂဏန်းအမှိုက် ထွက်လာတာလဲ
Union က နောက်ဆုံး ရေးခဲ့တဲ့ member ကို မမှတ်ထားပါဘူး။ Member နှစ်ခုလုံးက byte လေးခု တစ်ခုတည်းကို ယူထားလို့ i ကို ဖတ်တာက float 220.5 ရဲ့ bit pattern ကို integer အဖြစ် ပြန်အဓိပ္ပာယ်ကောက်တာပါ။ C က ဒီလို ပြန်အဓိပ္ပာယ်ကောက်တာ (type punning) ကို byte တွေက type အသစ်အတွက် မှန်ကန်တဲ့ value ဖြစ်နေသရွေ့ ခွင့်ပြုပေမယ့် ရလဒ်က platform က type နှစ်ခုကို ဘယ်လို ကိုယ်စားပြုလဲပေါ် မူတည်ပြီး ကိုယ်လိုချင်တဲ့ value မဟုတ်သလောက်ပါပဲ။ ဖြေရှင်းနည်းက ဘယ် member active ဖြစ်လဲ မှတ်တဲ့ သီးခြား tag တစ်ခု ထားပြီး ဖတ်တိုင်း မဖတ်ခင် စစ်တာပါ။
Core concept: နေရာ မျှသုံးခြင်းနဲ့ active member
Union declaration က union Data { int i; float f; char text[20]; }; လို struct လိုပဲ member တွေကို စာရင်းပြုစုပေမယ့် member တိုင်း offset 0 မှာ စပါတယ်။ ဒါကြောင့် union ရဲ့ အရွယ်အစားက အကြီးဆုံး member အရွယ်အစားဖြစ်ပြီး alignment အတင်းကျပ်ဆုံး member အတွက် သင့်တော်တဲ့ address မှာ ထားနိုင်အောင် ပိုကြီးအောင် ညှိပါတယ်။ x86-64 ပေါ်က GCC မှာ union Data က 20 byte ဖြစ်ပြီး member သုံးခု တူတူပါတဲ့ struct က 28 byte လိုပါတယ်။ union { char c[5]; int i; } ကတော့ 5 မဟုတ်ဘဲ 8 ဖြစ်ပါတယ်၊ အရွယ်အစားကို int ရဲ့ 4-byte alignment ရဲ့ ဆတိုးကိန်းအထိ တိုးလို့ပါ။
Member တစ်ခုကို ရေးလိုက်တာက အဲဒါကို active member ဖြစ်စေပါတယ်။ သူ့ byte တွေက တခြား member တိုင်းရဲ့ အစပိုင်းကို ထပ်ရေးပြီး ပိုကြီးတဲ့ member တွေမှာပဲ ပါတဲ့ byte တွေက unspecified value ဖြစ်သွားပါတယ်။ နောက်ဆုံး ရေးခဲ့တာ မဟုတ်တဲ့ member ကို ဖတ်တာကို C က ခွင့်ပြုပြီး type punning လို့ ခေါ်ပါတယ် — သိမ်းထားတဲ့ byte တွေကို type အသစ်အဖြစ် ပြန်အဓိပ္ပာယ်ကောက်တာမို့ ရလဒ်က platform ပေါ် မူတည်ပါတယ် (C++ မှာတော့ ဒီ code က undefined behaviour ပါ)။ သာမန် program တွေမှာ နောက်ဆုံး ရေးခဲ့တဲ့ member ကိုပဲ ဖတ်ပါ၊ float ရဲ့ byte တွေကို တကယ် ကြည့်ဖို့ လိုရင် memcpy နဲ့ integer ထဲ copy ကူးတာက ရည်ရွယ်ချက်ကို ပိုရှင်းစေပါတယ်။
ဘယ် member active ဖြစ်လဲဆိုတာ union က မပြောနိုင်လို့ တကယ့် code မှာ union ကို struct တစ်ခုထဲမှာ tag နဲ့ တွဲထားပါတယ် — struct Value { enum ValueKind kind; union { int i; double d; char text[16]; } as; };။ Enum (နောက် lesson မှာ အသေးစိတ် လေ့လာပါမယ်) က ဖြစ်နိုင်ခြေတွေကို နာမည်ပေးပြီး ရေးတိုင်း tag နဲ့ payload ကို အတူတူ သတ်မှတ်ကာ ဖတ်တိုင်း tag ပေါ် switch နဲ့ စပါတယ်။ ဒီ pattern ကို tagged union ဒါမှမဟုတ် discriminated union လို့ ခေါ်ပြီး Rust ရဲ့ enum လို language အများအပြားမှာ built-in ပါပါတယ်။ C မှာတော့ စည်းမျဉ်းကို ကိုယ်တိုင် ထိန်းရပါတယ်။
Union ကို brace နဲ့ initialize လုပ်လို့ရပေမယ့် ရိုးရိုး {value} က ပထမ member ကိုပဲ initialize လုပ်ပါတယ်။ union Data d = {220.5}; က 220.5 ကို int ပြောင်းပြီး d.i ထဲ 220 သိမ်းပါတယ်။ တခြား member ကို initialize လုပ်ချင်ရင် union Data d = {.f = 220.5f}; လို designated initializer နဲ့ နာမည်တပ်ပါ။
union Data { int i; float f; char text[20]; }; sizeof = 20
------------------------------------------------------------
bytes 0..3 bytes 4..19
+-----------+----------------------------+
view as i: | i | (not part of i) |
+-----------+----------------------------+
view as f: | f | (not part of f) |
+-----------+----------------------------+
view text: | text[0..19] |
+----------------------------------------+
three views of the same 20 bytes: all members start at offset 0,
and only the member written last holds a meaningful value- Member အားလုံး offset 0 ကို မျှသုံး
- Size = အကြီးဆုံး member (ညှိပြီး)
- တစ်ခု ရေးရင် ကျန်တာတွေ အစားထိုးခံရ
- ဘယ် member active လဲ tag က မှတ်
"နောက်ဆုံး ဘယ် member ကို ရေးခဲ့လဲ" ဆိုတာ code ကနေ မဖြေနိုင်ရင် tag တစ်ခု ထည့်ပါ။
အထွေထွေ ပုံစံ
union Name {
type1 member1;
type2 member2;
};
struct Tagged {
enum Kind kind; /* which member is active */
union Name as;
};
t.kind = KIND_ONE; t.as.member1 = value; /* write tag + payload */
switch (t.kind) { case KIND_ONE: use(t.as.member1); break; /* ... */ }Union ရဲ့ syntax က struct နဲ့ တူပေမယ့် member တွေက နေရာ မျှသုံးပါတယ်။ Tagged struct ထဲမှာ enum kind က active member ကို မှတ်ထားပြီး ရေးတိုင်း နှစ်ခုလုံး သတ်မှတ်ကာ ဖတ်တိုင်း kind ပေါ် switch လုပ်ပါတယ်။
Example: memory တစ်နေရာ၊ value သုံးမျိုး
ဒီ program က value အမျိုးအစား သုံးမျိုးကို union တစ်ခုတည်းထဲ တစ်ခုပြီးတစ်ခု ထည့်ပြီး ရေးပြီးပြီးချင်း ပြန်ဖတ်ပါတယ် — tag မပါတဲ့ union ကို သုံးတဲ့ ဘေးကင်းတဲ့ နည်းပါ။ Member တူတူပါတဲ့ struct နဲ့ union ရဲ့ အရွယ်အစားကိုလည်း နှိုင်းယှဉ်ပြပါတယ်။ Sizeof တွေက 64-bit Windows နဲ့ Linux ပေါ်က GCC မှာ တူပါတယ်။
#include <stdio.h>
union Data {
int i;
float f;
char text[20];
};
struct AllAtOnce {
int i;
float f;
char text[20];
};
int main(void) {
union Data data;
printf("sizeof(union Data) = %zu\n", sizeof(union Data));
printf("sizeof(struct AllAtOnce) = %zu\n", sizeof(struct AllAtOnce));
printf("members share one address: %s\n",
(void *)&data.i == (void *)&data.f ? "yes" : "no");
data.i = 10;
printf("data.i = %d\n", data.i);
data.f = 220.5f; /* overwrites the bytes that held i */
printf("data.f = %.1f\n", data.f);
snprintf(data.text, sizeof data.text, "%s", "hello");
printf("data.text = %s\n", data.text);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 3-7
union Data {Syntax က struct နဲ့ တူပေမယ့် i, f, text သုံးခုလုံး offset 0 မှာ စပြီး နေရာ တစ်ခုတည်းကို မျှသုံးပါတယ်။
- Lines 18-19
sizeof(union Data)Union က အကြီးဆုံး member text[20] အရွယ်အစား 20 byte ဖြစ်ပြီး struct AllAtOnce က 4 + 4 + 20 = 28 byte ပါ။
- Lines 20-21
(void *)&data.i == (void *)&data.f&data.i က int * ဖြစ်ပြီး &data.f က float * ဖြစ်လို့ (void *) ပြောင်းပြီးမှ နှိုင်းယှဉ်ပါတယ်။ Address နံပါတ်ကို မပြဘဲ yes ထွက်ပါတယ်။
- Line 26
data.f = 220.5f;အခု f က active member ဖြစ်ပြီး i ထဲက 10 က ပျောက်သွားပါပြီ။ Program က ဒီနောက်မှာ data.i ကို ဘယ်တော့မှ မဖတ်တာကို သတိထားကြည့်ပါ။
- Line 29
snprintf(data.text, sizeof data.text, "%s", "hello");text က active ဖြစ်လာပါတယ်။ snprintf က buffer အရွယ်အစား 20 ထက် ဘယ်တော့မှ ကျော်မရေးဘဲ string ကို '\0' နဲ့ အမြဲ ပိတ်ပေးပါတယ်။
sizeof(union Data) = 20
sizeof(struct AllAtOnce) = 28
members share one address: yes
data.i = 10
data.f = 220.5
data.text = helloလက်တွေ့ tagged union တစ်ခု
Spreadsheet ဒါမှမဟုတ် JSON parser လိုမျိုး လိုအပ်တဲ့ value အမျိုးမျိုး ရောနေတဲ့ row တစ်ခုမှာ tag-plus-payload pattern ကို သုံးထားပုံ ဒီမှာ ကြည့်ပါ။ Designated initializer တွေက tag နဲ့ ကိုက်ညီတဲ့ union member ကို တစ်ဆင့်တည်းနဲ့ သတ်မှတ်ပြီး print_value() က kind ကို မစစ်ဘဲ member တစ်ခုကိုမှ မထိပါဘူး။
#include <stdio.h>
enum ValueKind { VALUE_INT, VALUE_DOUBLE, VALUE_TEXT };
struct Value {
enum ValueKind kind; /* the tag: which member is active */
union {
int i;
double d;
char text[16];
} as;
};
void print_value(const struct Value *v) {
switch (v->kind) {
case VALUE_INT:
printf("int: %d\n", v->as.i);
break;
case VALUE_DOUBLE:
printf("double: %.2f\n", v->as.d);
break;
case VALUE_TEXT:
printf("text: %s\n", v->as.text);
break;
default:
printf("unknown kind %d\n", (int)v->kind);
break;
}
}
int main(void) {
struct Value row[3] = {
{.kind = VALUE_INT, .as.i = 42},
{.kind = VALUE_DOUBLE, .as.d = 3.75},
{.kind = VALUE_TEXT, .as.text = "Yangon"},
};
for (int n = 0; n < 3; n++) {
print_value(&row[n]);
}
printf("sizeof(struct Value) = %zu\n", sizeof(struct Value));
return 0;
}ဒီ program က int: 42၊ double: 3.75၊ text: Yangon ကို print ထုတ်ပါတယ်။ default branch က မျှော်လင့်ထားတဲ့ value တစ်ခုမှ မဟုတ်တဲ့ tag ကို ကိုင်တွယ်ပါတယ် — data ကို file ကနေ ဖတ်ထားတာ ဒါမှမဟုတ် bug ကြောင့် ပျက်စီးသွားတာမျိုးမှာ ဖြစ်နိုင်ပါတယ်။ x86-64 ပေါ်က GCC မှာ struct Value က 24 byte ပါ — tag အတွက် 4၊ double ကို 8-aligned ဖြစ်အောင် padding 4၊ union အတွက် 16 ပါ။
Union လား struct လား
| မေးခွန်း | union | struct |
|---|---|---|
| တစ်ပြိုင်တည်း member ဘယ်နှစ်ခုမှာ value ရှိလဲ | တစ်ခု — နောက်ဆုံး ရေးခဲ့တာ | အားလုံး |
| အရွယ်အစား | အကြီးဆုံး member (alignment အတွက် ညှိပြီး) | Member ပေါင်းလဒ် + padding |
| ပုံမှန် အသုံး | Tag နဲ့တွဲပြီး value အမျိုးအစား အမျိုးမျိုးထဲက တစ်ခု | ဂုဏ်သတ္တိ အများအပြားပါတဲ့ record |
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Value တစ်ခုက ပုံစံ အမျိုးမျိုးထဲက တစ်ခု ဖြစ်နိုင်တဲ့ နေရာတိုင်းမှာ tagged union တွေ ပေါ်လာပါတယ်။ Interpreter နဲ့ scripting engine တွေက runtime value တစ်ခုစီကို tag တစ်ခုနဲ့ number, string pointer, object pointer စတာတွေရဲ့ union အဖြစ် သိမ်းပါတယ်။ JSON နဲ့ configuration file parser တွေက number, string, array ဒါမှမဟုတ် object ဖြစ်တဲ့ node တွေကို ထုတ်ပေးပြီး compiler တွေကလည်း syntax tree node တွေကို အဲဒီနည်းအတိုင်း ကိုယ်စားပြုပါတယ်။
Operating system နဲ့ library တွေရဲ့ API မှာလည်း union တွေ သုံးပါတယ်။ SDL ရဲ့ SDL_Event က shared type field နဲ့ စတဲ့ event struct အများအပြားရဲ့ union ဖြစ်လို့ code က event.type ကို အရင်ဖတ်ပြီးမှ ကိုက်ညီတဲ့ member ကို ဖတ်ပါတယ်။ Network နဲ့ file-format code တွေက byte တစ်စုတည်းကို ပုံစံ အမျိုးမျိုးနဲ့ ကြည့်ဖို့ buffer ပေါ်မှာ union ကို ထပ်တင်တာ ရံဖန်ရံခါ လုပ်ကြပါတယ် — ဒီ lesson က representation စည်းမျဉ်းတွေ အရေးကြီးတဲ့ နေရာ အတိအကျပါပဲ၊ ဂရုစိုက်တဲ့ code တွေက byte တစ်ခုချင်း ရှင်းရှင်းလင်းလင်း parse လုပ်တာကို ပိုနှစ်သက်ကြပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. နောက်ဆုံး မရေးခဲ့တဲ့ member ကို ဖတ်တာ
data.f = 220.5f; ပြီးမှ data.i ကို print ထုတ်ရင် သာမန် PC မှာ 220 မဟုတ်ဘဲ 1130135552 ရပါတယ်။ Code က C အရ တရားဝင်လို့ compiler က warning မပေးပါဘူး။ Tag ထားပါ၊ တခြား type ရဲ့ ဂဏန်း တကယ် လိုချင်ရင် (int)data.f လို ရှင်းရှင်း ပြောင်းပါ။
2. မှားတဲ့ member ကို initialize လုပ်တာ
union Data d = {220.5}; က ပထမ member int ကို 220 နဲ့ တိတ်တိတ်လေး initialize လုပ်ပါတယ်။ -Wall -Wextra နဲ့တောင် ဘာမှ မပြောပါဘူး၊ -Wconversion (-Wfloat-conversion ကနေ) ကပဲ conversion က value ကို ပြောင်းစေကြောင်း report လုပ်ပါတယ်။ union Data d = {.f = 220.5f}; လို designated initializer ကို သုံးပါ။
3. Tag ကို မပြောင်းဘဲ payload ကိုပဲ ပြောင်းတာ
v.kind က VALUE_INT လို့ ပြောနေတုန်း v.as.d = 3.5; လို့ ရေးလိုက်ရင် struct က သူ့ပါဝင်မှုအကြောင်း လိမ်ပြောနေတာ ဖြစ်ပြီး နောက် print_value() က မှားတဲ့ member ကို ဖတ်ပါတယ်။ Tag နဲ့ payload ကို အတူတူ ပြောင်းတဲ့ setter function သေးသေးလေးတွေ ရေးပါ၊ ဒါမှ နှစ်ခု မကွဲသွားမှာပါ။
4. sizeof က အကြီးဆုံး member နဲ့ အမြဲ ညီတယ် ထင်တာ
union { char c[5]; int i; } က x86-64 မှာ 5 မဟုတ်ဘဲ 8 byte ပါ၊ အရွယ်အစားကို int ရဲ့ alignment အထိ တိုးလို့ပါ။ Union အရွယ်အစားကို လက်နဲ့ မတွက်ဘဲ sizeof ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Data အမျိုးအစား တစ်မျိုးထက် ပိုကိုင်တဲ့ union တိုင်းကို enum tag ပါတဲ့ struct ထဲ ထည့်ပါ။
- Tagged union တိုင်းအတွက် tag နဲ့ payload ကို အတူတူ သတ်မှတ်တဲ့ constructor ဒါမှမဟုတ် setter function တွေ ရေးပါ။
- Tag ပေါ်က switch တိုင်းမှာ ပျက်စီးနေတဲ့ ဒါမှမဟုတ် မမျှော်လင့်ထားတဲ့ value တွေကို ဖမ်းဖို့ default case ထည့်ပါ။
Mini challenge
Mini challenge: tagged union နဲ့ ပုံသဏ္ဌာန်များ
စက်ဝိုင်းနဲ့ စတုဂံတွေကို tag နဲ့ union ပါတဲ့ struct တစ်ခုတည်းနဲ့ ကိုယ်စားပြုပြီး သူတို့ရဲ့ ဧရိယာကို ဘေးကင်းကင်း တွက်ပါ။
- SHAPE_CIRCLE နဲ့ SHAPE_RECT ပါတဲ့ enum ShapeKind နဲ့ kind ပါပြီး circle (radius) နဲ့ rect (width, height) ပါတဲ့ union ကို ကိုင်တဲ့ struct Shape ရေးရမယ်
- make_circle(r) နဲ့ make_rect(w, h) က tag နဲ့ payload ကို အတူတူ သတ်မှတ်ထားတဲ့ Shape ကို တည်ဆောက်ရမယ်
- int area(const struct Shape *s, double *out) က ဧရိယာကို သိမ်းပြီး 1 ပြန်ရမယ်၊ မသိတဲ့ tag ဆိုရင် 0 ပြန်ရမယ်
- အချင်းဝက် 1 ရှိတဲ့ စက်ဝိုင်း၊ 2 x 3.5 စတုဂံ နဲ့ tag ကို 7 လို့ ပျက်စီးစေထားတဲ့ shape တစ်ခုရဲ့ ဧရိယာတွေကို print ထုတ်ရမယ်
#include <stdio.h>
#define PI 3.14159265358979
enum ShapeKind { SHAPE_CIRCLE, SHAPE_RECT };
struct Shape {
enum ShapeKind kind;
// TODO: a union named "as" with circle.radius and rect.width / rect.height
};
struct Shape make_circle(double radius) {
// TODO: set kind and as.circle.radius together
}
struct Shape make_rect(double width, double height) {
// TODO: set kind and as.rect together
}
int area(const struct Shape *s, double *out) {
// TODO: switch on s->kind; return 0 for an unknown kind
}
int main(void) {
// TODO: make three shapes, corrupt the third one's kind to 7, then print
// "shape N: area X.XX" or "shape N: unknown kind, skipped"
return 0;
}ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: shape 0: area 3.14 shape 1: area 7.00 shape 2: unknown kind, skipped
အရိပ်အမြွက် ကြည့်ရန်
Union ထဲမှာ struct { double radius; } circle; နဲ့ struct { double width, height; } rect; လို နာမည်ရှိတဲ့ struct နှစ်ခု ထည့်နိုင်ပါတယ်။ {.kind = SHAPE_CIRCLE, .as.circle.radius = radius} လို designated initializer က tag နဲ့ payload ကို တစ်ခါတည်း သတ်မှတ်ပါတယ်။ Tag ကို ပျက်စီးစေဖို့ shapes[2].kind = (enum ShapeKind)7; လို့ ရေးပါ။
အဖြေ ကြည့်ရန်
#include <stdio.h>
#define PI 3.14159265358979
enum ShapeKind { SHAPE_CIRCLE, SHAPE_RECT };
struct Shape {
enum ShapeKind kind;
union {
struct { double radius; } circle;
struct { double width, height; } rect;
} as;
};
struct Shape make_circle(double radius) {
struct Shape s = {.kind = SHAPE_CIRCLE, .as.circle.radius = radius};
return s;
}
struct Shape make_rect(double width, double height) {
struct Shape s = {.kind = SHAPE_RECT, .as.rect = {width, height}};
return s;
}
/* Returns 1 and stores the area, or 0 for an unknown tag. */
int area(const struct Shape *s, double *out) {
switch (s->kind) {
case SHAPE_CIRCLE:
*out = PI * s->as.circle.radius * s->as.circle.radius;
return 1;
case SHAPE_RECT:
*out = s->as.rect.width * s->as.rect.height;
return 1;
default:
return 0;
}
}
int main(void) {
struct Shape shapes[3] = {make_circle(1.0), make_rect(2.0, 3.5), make_circle(2.0)};
shapes[2].kind = (enum ShapeKind)7; /* simulate corrupted input */
for (int n = 0; n < 3; n++) {
double a = 0.0;
if (area(&shapes[n], &a)) {
printf("shape %d: area %.2f\n", n, a);
} else {
printf("shape %d: unknown kind, skipped\n", n);
}
}
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Union ရဲ့ member အားလုံး address တစ်ခုတည်းမှာ စလို့ တစ်ကြိမ်မှာ တစ်ခုပဲ အဓိပ္ပာယ်ရှိတဲ့ value ကိုင်ပါတယ်။
- Union ရဲ့ sizeof က အကြီးဆုံး member ဖြစ်ပြီး alignment အတင်းကျပ်ဆုံး member ရဲ့ ဆတိုးကိန်းအထိ တိုးပါတယ်။
- Union ကို struct ထဲမှာ enum tag နဲ့ တွဲပြီး နှစ်ခုလုံးကို အတူတူ သတ်မှတ်ကာ မဖတ်ခင် tag ပေါ် switch လုပ်ပါ။
- နောက်ဆုံး ရေးခဲ့တာ မဟုတ်တဲ့ member ကို ဖတ်တာက byte တွေကို ပြန်အဓိပ္ပာယ်ကောက်တာမို့ platform ပေါ် မူတည်ပါတယ် — သာမန် code မှာ ရှောင်ပါ။
- ရိုးရိုး brace initializer က ပထမ member ကို သတ်မှတ်ပါတယ်၊ တခြား member အတွက် .member = value ကို သုံးပါ။
SEI CERT C Coding Standard — Carnegie Mellon SEI