1. ã€ã³ãããã¯ã·ã§ã³
ããã°ã©ãã³ã°ã«ãããŠãã¡ã¢ãªå¹çã®åäžãè€éãªããŒã¿ç®¡çãè¡ãããã®ããŒã¿æ§é ã¯éåžžã«éèŠã§ããCèšèªã®ãå ±çšäœïŒunionïŒãã¯ãããããããŒãºã«å¿ããããã«èšèšãããããŒã¿åã®ã²ãšã€ã§ããå ±çšäœãå©çšããããšã§ãã¡ã¢ãªäœ¿çšéãæããç°ãªãããŒã¿åã®å€ãå¹ççã«ç®¡çããããšãå¯èœã«ãªããŸãã
å ±çšäœã®ç¹åŸŽãšç®ç
å ±çšäœã¯ãè€æ°ã®ã¡ã³ããŒãåãã¡ã¢ãªé åãå ±æããããŒã¿æ§é ã§ããæ§é äœïŒstructïŒãåã¡ã³ããŒã«åå¥ã®ã¡ã¢ãªé åã確ä¿ããã®ã«å¯Ÿããå ±çšäœã§ã¯è€æ°ã®ã¡ã³ããŒãå ±éã®ã¡ã¢ãªã䜿çšããŸãããã®ãããç°ãªãããŒã¿åãå¹ççã«æ±ãããšãå¯èœã§ããç¹ã«ã¡ã¢ãªå®¹éãéãããŠããçµã¿èŸŒã¿ã·ã¹ãã ãªã©ã§å©çšãããããšãå€ãããããã¯ãŒã¯éä¿¡ãããŒã¿ãã±ããã®è§£æã«ãããŠãéå®ãããŠããŸãã
å ±çšäœãæ±ããããå Žé¢
å ±çšäœã®å©ç¹ã¯ããåãã¡ã¢ãªé åãç°ãªãæ¹æ³ã§è§£éããããšããç¹åŸŽã«ãããŸããäŸãã°ããããã¯ãŒã¯ããã°ã©ãã³ã°ã§ã¯ãããŒã¿ãã±ããã®äžã«ç°ãªãæ å ±ãè©°ãŸã£ãŠãããããããã®èŠçŽ ã«ã¢ã¯ã»ã¹ããå¿ èŠããããŸããå ±çšäœã䜿çšããããšã§ãã²ãšã€ã®ããŒã¿ãè€æ°ã®èŠç¹ããæ±ãããšãã§ãããããã¡ã¢ãªå¹çãšå¯èªæ§ãä¿ã¡ãªãããå¿ èŠãªããŒã¿ã«ã¢ã¯ã»ã¹ã§ããã®ã§ãã
ãŸããå ±çšäœã¯ãã¿ã°ä»ãå ±çšäœããšããŠããã䜿ãããŸããã¿ã°ä»ãå ±çšäœã§ã¯ãå ±çšäœå ã§ç°ãªãããŒã¿åã®ãã¡ããããäžã€ã ããæ ŒçŽããã¡ã¢ãªæ¶è²»ãåæžãã€ã€ãåã®ç®¡çãè¡ããŸãããã®ã¿ã°ä»ãå ±çšäœã¯ãç¹ã«ã¡ã¢ãªã®å¹çåãæ±ããããã±ãŒã¹ããéãããã¡ã¢ãªå ã§è€æ°ã®ããŒã¿åãæ±ãå¿ èŠãããå Žåã«æå¹ã§ãã
æ§é äœãšã®éã
å ±çšäœãšæ§é äœã¯äŒŒããããªæ§æãæã€ããæ··åãããã¡ã§ãããã¡ã¢ãªã®äœ¿ãæ¹ã倧ããç°ãªããŸããæ§é äœã¯ãã¹ãŠã®ã¡ã³ããŒãããããç¬èªã®ã¡ã¢ãªé åãæã¡ãããŒã¿ã®å€æŽãä»ã®ã¡ã³ããŒã«åœ±é¿ãäžããããšã¯ãããŸãããäžæ¹ãå ±çšäœã§ã¯ããã¹ãŠã®ã¡ã³ããŒãåäžã®ã¡ã¢ãªé åãå ±æããŸãããã®ãããã²ãšã€ã®ã¡ã³ããŒã«å€ãèšå®ãããšãä»ã®ã¡ã³ããŒã«ããã®åœ±é¿ãåã¶ãšããç¹åŸŽããããŸãã
2. å ±çšäœã®åºæ¬æ§æãšå®£èšæ¹æ³
å ±çšäœïŒunionïŒã¯ãCèšèªã«ãããããŒã¿æ§é ã®äžçš®ã§ãããè€æ°ã®ããŒã¿åã®ã¡ã³ããŒãåãã¡ã¢ãªé åãå ±æããç¹åŸŽãæã¡ãŸãããã®ç« ã§ã¯ãå ±çšäœãã©ã®ããã«å®çŸ©ããå©çšããã®ãã«ã€ããŠåºæ¬çãªæ§æãšå®£èšæ¹æ³ã解説ããŸãã
å ±çšäœã®å®£èšæ¹æ³
å
±çšäœã¯ãæ§é äœãšåæ§ã«union
ããŒã¯ãŒããçšããŠå®£èšããŸããæ§æãšããŠã¯ã以äžã®ãããªåœ¢ã«ãªããŸãã
union å
±çšäœå {
ããŒã¿å ã¡ã³ããŒ1;
ããŒã¿å ã¡ã³ããŒ2;
...
};
äŸïŒå ±çšäœã®å®£èš
以äžã®ã³ãŒãã¯ãExample
ãšããååã®å
±çšäœã宣èšããæŽæ°åãæµ®åå°æ°ç¹åãæååã®ã¡ã³ããŒãæã€äŸã§ãã
union Example {
int integer;
float decimal;
char character;
};
ãã®å ±çšäœã¯ãæŽæ°ãæµ®åå°æ°ç¹ãæåã®ããããäžã€ã®å€ãæ ŒçŽããããšãã§ããŸããåãã¡ã¢ãªé åã«è€æ°ã®ã¡ã³ããŒãå®çŸ©ããããããäžåºŠã«ä¿æã§ããå€ã¯äžã€ã ãã§ãæåŸã«æ ŒçŽããã¡ã³ããŒã®å€ã®ã¿ãæå¹ãšãªããŸãã
å ±çšäœã®åæå
å
±çšäœã®å€æ°ãåæåããéã«ã¯ãæ§é äœãšåãããã«äžæ¬åŒ§{ }
ãçšããŸãã以äžã¯ãå
±çšäœData
ãåæåããéã®äŸã§ãã
union Data {
int id;
float salary;
char name[20];
};
int main() {
union Data data = { .id = 123 };
printf("ID: %d\n", data.id);
return 0;
}
ãã®äŸã§ã¯ãæåã®ã¡ã³ããŒid
ã«å€ãèšå®ããŠããŸããå
±çšäœã¯ãæåã«æå®ãããã¡ã³ããŒã®åã«åŸã£ãŠåæåãè¡ããããããä»ã®ã¡ã³ããŒã«ã¯åœ±é¿ãäžããŸããã
å ±çšäœã®ã¡ã³ããŒãžã®ã¢ã¯ã»ã¹
å
±çšäœã®ã¡ã³ããŒã«ã¢ã¯ã»ã¹ããã«ã¯ããããæŒç®åïŒ.
ïŒã䜿çšããŸããå€æ°åã®åŸã«ãããæŒç®åãã€ããŠãã¢ã¯ã»ã¹ãããã¡ã³ããŒãæå®ããã ãã§ãç°¡åã«å€ã®èšå®ãååŸãå¯èœã§ãã
äŸïŒã¡ã³ããŒãžã®ã¢ã¯ã»ã¹
union Data {
int id;
float salary;
char name[20];
};
int main() {
union Data data;
// ã¡ã³ããŒãžã®ã¢ã¯ã»ã¹
data.id = 101;
printf("ID: %d\n", data.id);
data.salary = 50000.50;
printf("Salary: %.2f\n", data.salary);
snprintf(data.name, sizeof(data.name), "Alice");
printf("Name: %s\n", data.name);
return 0;
}
ãã®äŸã§ã¯ãå
±çšäœData
ã®åã¡ã³ããŒã«å€ãèšå®ããŠè¡šç€ºããŠããŸãããã ããå
±çšäœã¯åäžã®ã¡ã¢ãªãå
±æãããããæåŸã«èšå®ããã¡ã³ããŒã®å€ã®ã¿ãæå¹ã§ããããšã«æ³šæãå¿
èŠã§ãã
å ±çšäœãšæ§é äœã®å®£èšã®éã
å ±çšäœãšæ§é äœã¯åããããªå®£èšæ§æãæã¡ãŸãããã¡ã¢ãªã®å²ãåœãŠæ¹æ³ã«å€§ããªéãããããŸããæ§é äœã§ã¯åã¡ã³ããŒã«ç¬ç«ããã¡ã¢ãªé åãå²ãåœãŠãããŸãããå ±çšäœã§ã¯ãã¹ãŠã®ã¡ã³ããŒãåãã¡ã¢ãªé åã䜿çšããŸãããã®ãããå ±çšäœã®ã¡ã¢ãªãµã€ãºã¯ãã¡ã³ããŒã®äžã§æã倧ããªãµã€ãºã®ãã®ã«åºã¥ããŠæ±ºãŸããŸãã
ã¡ã¢ãªãµã€ãºã®ç¢ºèª
å
±çšäœã®ã¡ã¢ãªãµã€ãºã確èªããã«ã¯ãsizeof
æŒç®åã䜿çšããŸãã以äžã¯ãå
±çšäœData
ã®ã¡ã¢ãªãµã€ãºã確èªããäŸã§ãã
#include <stdio.h>
union Data {
int id;
float salary;
char name[20];
};
int main() {
printf("å
±çšäœã®ãµã€ãº: %zu ãã€ã\n", sizeof(union Data));
return 0;
}
ãã®äŸã§ã®Data
å
±çšäœã¯ãname
ã¡ã³ããŒã®ãµã€ãºã«åºã¥ããŠ20ãã€ãã®ã¡ã¢ãªãå²ãåœãŠãŠããŸããã¡ã³ããŒã®äžã§æã倧ãããµã€ãºãã¡ã¢ãªå
šäœã«å²ãåœãŠããããããç¡é§ãªãå¹ççãªã¡ã¢ãªäœ¿çšãå¯èœã§ãã
3. å ±çšäœã®ç¹æ§ãšã¡ã¢ãªç®¡ç
Cèšèªã®å ±çšäœïŒunionïŒã¯ãè€æ°ã®ã¡ã³ããŒãåãã¡ã¢ãªé åãå ±æããããšã§ãã¡ã¢ãªäœ¿çšéãå¹ççã«æããç¹åŸŽãæã£ãŠããŸãããã®ç« ã§ã¯ãå ±çšäœãæã€ç¹æ§ãšãã¡ã¢ãªç®¡çã®ä»çµã¿ã«ã€ããŠè§£èª¬ããŸãã
åãã¡ã¢ãªé åãå ±æããä»çµã¿
å ±çšäœã¯ãæ§é äœãšäŒŒã宣èšæ§æãæã¡ãªããããã¡ã¢ãªã®å²ãåœãŠæ¹ã«å€§ããªéãããããŸããæ§é äœã§ã¯ã¡ã³ããŒããšã«ç¬ç«ããã¡ã¢ãªé åã確ä¿ãããŸãããå ±çšäœã§ã¯ãã¹ãŠã®ã¡ã³ããŒãåäžã®ã¡ã¢ãªé åãå ±æããŸãããã®ãããåæã«è€æ°ã®ã¡ã³ããŒã«ç°ãªãããŒã¿ãæ ŒçŽããããšã¯ã§ãããæåŸã«èšå®ããã¡ã³ããŒã®å€ã®ã¿ãæå¹ã§ãã
ã¡ã¢ãªé 眮ã®ã€ã¡ãŒãž
äŸãã°ã以äžã®å
±çšäœExample
ã§ã¯ãintå
ãfloatå
ãcharå
ãåãã¡ã¢ãªãå
±æããŸãã
union Example {
int integer;
float decimal;
char character;
};
ãã®å
±çšäœã®ãµã€ãºã¯ã3ã€ã®ã¡ã³ããŒã®äžã§æã倧ããã¡ã¢ãªãµã€ãºïŒãã®å Žåãint
ãfloat
ïŒã«åºã¥ããŠæ±ºãŸããŸããã€ãŸããå
±çšäœã®ãµã€ãºã¯æ倧ã®ã¡ã³ããŒã«äŸåããŠãããä»ã®ã¡ã³ããŒããã®ã¡ã¢ãªé åå
ã§ããŒã¿ãå
±æããããšã«ãªããŸãã
å ±çšäœã®ãµã€ãºã確èªãã
å
±çšäœã®ã¡ã¢ãªãµã€ãºã確èªããã«ã¯ãsizeof
æŒç®åã䜿çšããŸããäŸãã°ã以äžã®ã³ãŒãã§å
±çšäœData
ã®ãµã€ãºãååŸã§ããŸãã
#include <stdio.h>
union Data {
int id;
float salary;
char name[20];
};
int main() {
printf("å
±çšäœã®ãµã€ãº: %zu ãã€ã\n", sizeof(union Data));
return 0;
}
ãã®äŸã§ã¯ãname
ãæã倧ãããµã€ãºïŒ20ãã€ãïŒãæã€ãããå
±çšäœå
šäœã®ãµã€ãºã¯20ãã€ããšãªããŸããä»ã®ã¡ã³ããŒã¯ãã®é åå
ã§å€ãå
±æãã圢ãšãªããããã¡ã¢ãªäœ¿çšéãæå°éã«æããããŸãã
ã¿ã°ä»ãå ±çšäœã«ããå管ç
å ±çšäœã¯åãã¡ã¢ãªé åãè€æ°ã®ããŒã¿åã§å ±æãããããã©ã®ããŒã¿åãæåŸã«äœ¿çšããããã管çããå¿ èŠããããŸããããã«ã¯ããã¿ã°ä»ãå ±çšäœããšããææ³ãããçšããããŸããã¿ã°ä»ãå ±çšäœã¯ãã¡ã³ããŒã®ã©ããæåŸã«æå¹ã§ãããã瀺ãã¿ã°å€æ°ãå ±çšäœãšäžç·ã«æã€æ§é äœã§ããããã«ãããçŸåšã®ããŒã¿åãæ確ã«ç®¡çã§ããããã°ã©ã ã®å¯èªæ§ãä¿¡é Œæ§ãåäžããŸãã
ã¿ã°ä»ãå ±çšäœã®äŸ
以äžã«ãã¿ã°ä»ãå ±çšäœã䜿çšããŠç°ãªãããŒã¿åã管çããäŸã瀺ããŸãã
#include <stdio.h>
enum Type { INTEGER, FLOAT, STRING };
struct TaggedUnion {
enum Type type;
union {
int intValue;
float floatValue;
char strValue[20];
} data;
};
int main() {
struct TaggedUnion tu;
// INTEGERåã®å€ãã»ãã
tu.type = INTEGER;
tu.data.intValue = 42;
// åã確èªããŠåºå
if (tu.type == INTEGER) {
printf("æŽæ°å€: %d\n", tu.data.intValue);
}
return 0;
}
ãã®äŸã§ã¯ãTaggedUnion
æ§é äœã«å
±çšäœdata
ãšåæ
å ±ãæ ŒçŽããtype
ãæãããããšã§ãçŸåšæå¹ãªããŒã¿åã管çããŠããŸããtype
ã«ãã£ãŠã¡ã³ããŒã®ããŒã¿åãæ確ã«ãªããããæå³ããªãããŒã¿åãžã®ã¢ã¯ã»ã¹ãé²æ¢ã§ããŸãã
ã¡ã¢ãªç®¡çã«ããã泚æç¹
å ±çšäœã䜿çšããéã«ã¯ãããã€ãã®æ³šæç¹ããããŸããç¹ã«ãã¡ã¢ãªãªãŒããŒã©ããã«ããäºæããªãåäœãçºçãããããããŒã¿ã®å管çãéèŠã§ãã
ã¡ã¢ãªãªãŒããŒã©ããã®ãªã¹ã¯
å ±çšäœã®ã¡ã³ããŒãåäžã¡ã¢ãªãå ±æããããšã§ãããã¡ã³ããŒã«å€ãèšå®ããåŸã«å¥ã®ã¡ã³ããŒã«ã¢ã¯ã»ã¹ãããšãäºæããªãçµæãåŸãããå¯èœæ§ããããŸããããšãã°ãæŽæ°å€ãèšå®ããåŸã«æµ®åå°æ°ç¹æ°ãšããŠã¢ã¯ã»ã¹ããå Žåãæ£ããå€ãåŸãããªãããšãå€ãã§ãã
#include <stdio.h>
union Example {
int intValue;
float floatValue;
};
int main() {
union Example example;
example.intValue = 42;
printf("æµ®åå°æ°ç¹æ°ãšããŠã®å€: %f\n", example.floatValue); // äžæ£ãªå€ã衚瀺ãããå¯èœæ§ãã
return 0;
}
ãã®ããã«ãã¡ã¢ãªãªãŒããŒã©ããã«ãã£ãŠããŒã¿ãäžæ£ç¢ºã«è§£éãããããšããããããå ±çšäœã®äœ¿çšã«ã¯ååãªæ³šæãå¿ èŠã§ãã
åå®å šæ§ã®ç¢ºä¿
å ±çšäœã«ã¯åå®å šæ§ããããŸãããå ±çšäœã®ã¡ã³ããŒã«ã¢ã¯ã»ã¹ããéãçŸåšæå¹ãªããŒã¿åã管çãã責任ã¯ããã°ã©ãã«ãããŸãã誀ã£ãããŒã¿åã§ã¢ã¯ã»ã¹ãããšãããŒã¿ãç Žæãããªã¹ã¯ããããããã¿ã°ä»ãå ±çšäœã®ãããªå·¥å€«ã§åã管çããããšãæšå¥šãããŸãã
å ±çšäœã䜿ãã¡ãªãã
å ±çšäœã¯ãã¡ã¢ãªå¹çãéèŠããããã°ã©ãã³ã°ã«ãããŠéåžžã«æçšã§ããã¡ã¢ãªé åãäžã€ã®ã¡ã³ããŒã§ã®ã¿å æãããããã¡ã¢ãªå®¹éãéãããŠããç°å¢ïŒçµã¿èŸŒã¿ã·ã¹ãã ããããã¯ãŒã¯éä¿¡ãªã©ïŒã§ã®å©çšãäžè¬çã§ãã䜿çšããéã«ã¯ãå管çã®å·¥å€«ãã¡ã¢ãªãªãŒããŒã©ããã®ãªã¹ã¯ãèæ ®ããå ±çšäœã®ç¹åŸŽã掻ãããèšèšãæ±ããããŸãã
4. å ±çšäœã®å©çšå Žé¢ãšå®è·µäŸ
å ±çšäœïŒunionïŒã¯ãã¡ã¢ãªå¹çãæ±ããããå Žé¢ã§ç¹ã«åœ¹ç«ã€ããŒã¿æ§é ã§ããããã§ã¯ãå ±çšäœãå®éã«æŽ»çšãããã±ãŒã¹ããã®å¿çšäŸãå ·äœçã«çŽ¹ä»ããŸããå ±çšäœãå¹æçã«äœ¿ãããšã§ãã¡ã¢ãªã®ç¯çŽãããŒã¿ç®¡çã®å¹çåãå¯èœã«ãªããŸãã
ã¿ã°ä»ãå ±çšäœã®å©çš
ãã¿ã°ä»ãå ±çšäœãã¯ãå ±çšäœã®ã¡ã³ããŒã«è€æ°ã®ããŒã¿åãããå Žåã«ãã©ã®ããŒã¿åãçŸåšæå¹ã§ãããã管çããããã®å·¥å€«ã§ããã¿ã°å€æ°ãæã€ããšã§ãåã¡ã³ããŒã®ããŒã¿åãå®å šã«ç®¡çããäžèŠãªãšã©ãŒãåé¿ã§ããŸãã
ã¿ã°ä»ãå ±çšäœã®äŸ
以äžã®äŸã§ã¯ãå ±çšäœãšã¿ã°ãçµã¿åãããŠãæŽæ°ãæµ®åå°æ°ç¹æ°ãæååã®ãããããæ ŒçŽããããŒã¿æ§é ãå®çŸããŠããŸãã
#include <stdio.h>
enum DataType { INTEGER, FLOAT, STRING };
struct TaggedData {
enum DataType type;
union {
int intValue;
float floatValue;
char strValue[20];
} data;
};
int main() {
struct TaggedData td;
// æŽæ°ããŒã¿ãèšå®
td.type = INTEGER;
td.data.intValue = 42;
// ã¿ã°ã確èªããŠé©åã«åºå
if (td.type == INTEGER) {
printf("æŽæ°ããŒã¿: %d\n", td.data.intValue);
}
return 0;
}
ãã®ã³ãŒãã§ã¯ãã¿ã°type
ãINTEGER
ã§ãããã©ããã確èªããŠããæŽæ°ããŒã¿ãåºåããŠããŸããããã«ãããå
±çšäœã®çŸåšã®ããŒã¿åãæ確ã«ç®¡çã§ãããããå®å
šãã€å¹æçãªããŒã¿æäœãå¯èœã§ãã
ãããã¯ãŒã¯ããã°ã©ãã³ã°ã«ãããããŒã¿ãã±ãã解æ
ãããã¯ãŒã¯ããã°ã©ãã³ã°ãéä¿¡ãããã³ã«ã®å®è£ ã§ã¯ãããŒã¿ãã±ããã®å 容ãå¹ççã«åŠçããå¿ èŠããããŸããå ±çšäœã䜿ãããšã§ãåãã¡ã¢ãªé åã«ç°ãªã圢åŒã®ããŒã¿ãå¹çããæ ŒçŽãã解æããããšãã§ããŸãã
ããŒã¿ãã±ãã解æã®äŸ
以äžã¯ãå
±çšäœã䜿ã£ãŠããŒã¿ãã±ããã®å
容ã解æããæ¹æ³ã®äŸã§ããããŒã¿å
šäœãfullPacket
ãšããŠæ±ãã€ã€ãåãã±ããéšåã«ã¢ã¯ã»ã¹ã§ããŸãã
#include <stdio.h>
union Packet {
struct {
unsigned char header;
unsigned char payload[3];
} parts;
unsigned int fullPacket;
};
int main() {
union Packet packet;
packet.fullPacket = 0xAABBCCDD; // ãã±ããå
šäœã®ããŒã¿
printf("ããããŒ: 0x%X\n", packet.parts.header);
printf("ãã€ããŒã: 0x%X 0x%X 0x%X\n", packet.parts.payload[0], packet.parts.payload[1], packet.parts.payload[2]);
return 0;
}
ãã®äŸã§ã¯ãfullPacket
ã«èšå®ãããããŒã¿ãparts
æ§é äœããåå¥ã«ã¢ã¯ã»ã¹ããŠããŸããããããããšã§ãã¡ã¢ãªãç¯çŽãã€ã€ãå¿
èŠã«å¿ããŠããŒã¿ãããŸããŸãªèŠç¹ã§è§£æã§ããŸãã
å¥ã®ããŒã¿åãžã®å解éïŒã¡ã¢ãªå解éïŒ
å ±çšäœãçšããããšã§ãã¡ã¢ãªã®ãã€ãåãç°ãªãããŒã¿åã§å解éããããšãå¯èœã§ããããšãã°ãæ°å€ãæååãšããŠè§£éããããæµ®åå°æ°ç¹æ°ãæŽæ°ãšããŠæ±ã£ããããéã«å©çšãããŸãã
ã¡ã¢ãªå解éã®äŸ
以äžã®äŸã§ã¯ãå ±çšäœãå©çšããŠã¡ã¢ãªäžã®ããŒã¿ãç°ãªãåã§èªã¿åºãäŸã§ãã
#include <stdio.h>
union Converter {
int num;
char bytes[4];
};
int main() {
union Converter converter;
converter.num = 0x12345678;
printf("ã¡ã¢ãªã®ãã€ãè¡šçŸ:\n");
for (int i = 0; i < 4; i++) {
printf("ãã€ã%d: 0x%X\n", i, (unsigned char)converter.bytes[i]);
}
return 0;
}
ãã®äŸã§ã¯ãæŽæ°å€0x12345678
ãbytes
ã¡ã³ããŒãããã€ãåäœã§èªã¿åºããŠããŸããå
±çšäœã䜿çšããããšã§ãåãã¡ã¢ãªé åãç°ãªãåãšããŠè§£éããå¹ççã«ããŒã¿ãæäœã§ããŸãã
å ±çšäœã䜿çšããéã®æ³šæç¹
å ±çšäœã掻çšããããšã§å¹ççãªã¡ã¢ãªç®¡çãå¯èœã«ãªããŸãããåæã«ãªã¹ã¯ãååšããŸããç¹ã«ãã¡ã¢ãªãªãŒããŒã©ãããåå®å šæ§ã®ç¢ºä¿ã«ã¯æ³šæãå¿ èŠã§ããäŸãã°ãæŽæ°ãšããŠèšå®ããããŒã¿ãæµ®åå°æ°ç¹ãšããŠèªã¿åºããå Žåãæå³ããªãå€ãåŸãããå¯èœæ§ããããããæ£ããåã§ã®ã¢ã¯ã»ã¹ãå¿ãããå¿ èŠããããŸãã
5. å ±çšäœã®æŽ»çšæã®æ³šæç¹ãšãªã¹ã¯ç®¡ç
å ±çšäœïŒunionïŒã¯ãCèšèªã§ã¡ã¢ãªå¹çãéèŠããããŒã¿ç®¡çã«æçšãªæ©èœã§ãããæ£ãã䜿ããªããšæå³ããªãåäœãåŒãèµ·ããå¯èœæ§ããããŸãããã®ç« ã§ã¯ãå ±çšäœã®æŽ»çšæã«ç¹ã«æ³šæãã¹ããã€ã³ããããªã¹ã¯ãæå°éã«æããæ¹æ³ã«ã€ããŠè§£èª¬ããŸãã
ã¡ã¢ãªãªãŒããŒã©ããã®ãªã¹ã¯
å ±çšäœã¯ããã¹ãŠã®ã¡ã³ããŒãåãã¡ã¢ãªé åãå ±æãããããããã¡ã³ããŒã«å€ãä»£å ¥ããåŸã«å¥ã®ã¡ã³ããŒãèªã¿åããšãäºæããªãå€ãåŸãããå¯èœæ§ããããŸããããã¯ãã¡ã¢ãªãªãŒããŒã©ããããšåŒã°ããç¹ã«ç°ãªãããŒã¿åã®ã¡ã³ããŒãæã€å ±çšäœã§çºçããããåé¡ã§ãã
ã¡ã¢ãªãªãŒããŒã©ããã®äŸ
以äžã®äŸã§ã¯ãæŽæ°ãšããŠèšå®ããå€ãæµ®åå°æ°ç¹æ°ãšããŠèªã¿åãããšã§ãäžæ£ãªããŒã¿ã衚瀺ãããå¯èœæ§ããããŸãã
#include <stdio.h>
union Example {
int intValue;
float floatValue;
};
int main() {
union Example example;
example.intValue = 42; // æŽæ°ãšããŠèšå®
printf("æµ®åå°æ°ç¹æ°ãšããŠã®å€: %f\n", example.floatValue); // äžæ£ãªå€ã衚瀺ãããå¯èœæ§
return 0;
}
ãã®ã³ãŒãã§ã¯ãintValue
ã«èšå®ããå€ãfloatValue
ãšããŠèªã¿åããšãäºæž¬äžèœãªå€ãåºåãããå¯èœæ§ããããŸããå
±çšäœã¯ç°ãªãåã§ã¡ã¢ãªãå
±æãããããåã®ç®¡çãæ
éã«è¡ãå¿
èŠããããŸãã
åå®å šæ§ã«é¢ããåé¡
å ±çšäœã¯åã®å®å šæ§ãä¿èšŒããŸãããå ±çšäœã®ã¡ã³ããŒã«ã¢ã¯ã»ã¹ããéãã©ã®åãçŸåšæå¹ã§ããããããã°ã©ãã管çãã責任ãçããŸãã誀ã£ãåã§ã¢ã¯ã»ã¹ãããšãããŒã¿ãç Žæãããªã¹ã¯ããããããåãå®å šã«ç®¡çãã工倫ãå¿ èŠã§ãã
åå®å šæ§ãä¿ã€ããã®å¯ŸçïŒã¿ã°ä»ãå ±çšäœ
ã¿ã°ä»ãå ±çšäœã䜿ãããšã§ãå ±çšäœã®ã¡ã³ããŒãæã€åã远跡ããé©åãªåã§ã¢ã¯ã»ã¹ããããšãå¯èœã«ãªããŸããã¿ã°ä»ãå ±çšäœã§ã¯ãå ±çšäœãšäžç·ã«çŸåšã®ããŒã¿åã瀺ããã¿ã°ããæãããããšã§ãåå®å šæ§ã確ä¿ã§ããŸãã
#include <stdio.h>
enum DataType { INTEGER, FLOAT };
struct TaggedUnion {
enum DataType type;
union {
int intValue;
float floatValue;
} data;
};
int main() {
struct TaggedUnion tu;
tu.type = INTEGER;
tu.data.intValue = 42;
if (tu.type == INTEGER) {
printf("æŽæ°å€: %d\n", tu.data.intValue);
} else if (tu.type == FLOAT) {
printf("æµ®åå°æ°ç¹å€: %f\n", tu.data.floatValue);
}
return 0;
}
ãã®ã³ãŒãã§ã¯ãã¿ã°type
ã«INTEGER
ãŸãã¯FLOAT
ãæ ŒçŽããããšã§ãçŸåšã®ããŒã¿åã管çããŠããŸããããã«ãããééã£ãåã§ã¢ã¯ã»ã¹ãããªã¹ã¯ãæžãããåã®å®å
šæ§ãé«ããããšãã§ããŸãã
ãããã°ã®é£ãã
å ±çšäœã䜿çšããã³ãŒãã¯ããããã°ãé£ãããªãããšãå€ãã§ããããã¯ãåãã¡ã¢ãªé åãè€æ°ã®åãå ±æãããããã©ã®ã¡ã³ããŒãçŸåšæå¹ã§ããããç¹å®ããã®ãé£ããããã§ãããã®ãããªç¹æ§ãããç¹ã«è€éãªã·ã¹ãã ã§ã¯å ±çšäœã®äœ¿çšã«æ³šæãå¿ èŠã§ãã
ãããã°ã容æã«ããããã®ãã€ã³ã
- ã¡ã¢ãªã®ç¶æ ã確èªããïŒãããã°æã«ã¯ãã¡ã¢ãªã®ç¶æ ã確èªããŠãã©ã®ã¡ã³ããŒãæåŸã«èšå®ãããããææ¡ããããšãéèŠã§ãã
- ã³ã¡ã³ããããã¥ã¡ã³ãã®æŽ»çšïŒå ±çšäœã®ã¡ã³ããŒãã©ã®ããã«å©çšããããã䜿çšæå³ã泚æç¹ãæ確ã«ã³ã¡ã³ããšããŠæ®ãããšã§ãä»ã®éçºè ãã³ãŒããç解ãããããªããŸãã
- ã¿ã°ä»ãå ±çšäœã®æŽ»çšïŒã¿ã°ä»ãå ±çšäœãçšããããšã§ãåã®å®å šæ§ãä¿èšŒãããããã°ã®æéãæžããããšãã§ããŸãã
ã¡ã¢ãªç®¡çã®èŠ³ç¹ããã®æ³šæç¹
å ±çšäœã®ã¡ã¢ãªãµã€ãºã¯ãã¡ã³ããŒã®äžã§æã倧ããªãµã€ãºã«åºã¥ããŠæ±ºãŸããŸãããç°ãªããã©ãããã©ãŒã ã§ã¯ã¡ã¢ãªã®é 眮ãåã®ãµã€ãºãç°ãªããããç°å¢ã«äŸåããåäœãé¿ããããšãéèŠã§ãã
ç°å¢äŸåæ§ã®åé¡
ç°ãªããã©ãããã©ãŒã éã§å ±çšäœãæ±ãå ŽåãããŒã¿åã®ãµã€ãºãã¡ã¢ãªé 眮ãå€ããããšã§ãäºæããªãåäœãçããå¯èœæ§ããããŸããç°å¢äŸåæ§ãæããããã«ã¯ããã©ãããã©ãŒã ããšã®ä»æ§ã«ç²Ÿéãããã¹ããè¡ãããšãéèŠã§ãã
å ±çšäœãå®å šã«å©çšããããã®ãŸãšã
- åå®å šæ§ã確ä¿ïŒã¿ã°ä»ãå ±çšäœãªã©ã掻çšããŠãçŸåšã®ããŒã¿åãé©åã«ç®¡çããŸãã
- ãããã°ã®å·¥å€«ïŒã³ã¡ã³ããã¡ã¢ãªã®ç¶æ 確èªãè¡ãããããã°ããããã³ãŒããå¿ãããŸãã
- ç°å¢äŸåã«æ³šæïŒç°ãªããã©ãããã©ãŒã ã§åäœããããã°ã©ã ã§ã¯ãå ±çšäœã®äœ¿ãæ¹ã«æ³šæãæããŸãã
6. ãŸãšããšå®çšäžã®ãã€ã³ã
å ±çšäœïŒunionïŒã¯ãCèšèªã§ã¡ã¢ãªå¹çãæé©åããªããç°ãªãããŒã¿åã管çããããã®éèŠãªããŒã¿æ§é ã§ãããã®èšäºã§ã¯ãå ±çšäœã®åºæ¬çãªå®£èšæ¹æ³ããã¡ã¢ãªç®¡çãå®éã®å©çšå Žé¢ã泚æç¹ã«è³ããŸã§è©³ãã解説ããŠããŸããããã®ç« ã§ã¯ãå ±çšäœã®å©çšæã«ç¹ã«æèãã¹ãéèŠãªãã€ã³ããæ¯ãè¿ããå®çšçãªã¢ããã€ã¹ããŸãšããŸãã
å ±çšäœã䜿ãã¡ãªããã®å確èª
å ±çšäœã®æ倧ã®ã¡ãªããã¯ãç°ãªãããŒã¿åãåãã¡ã¢ãªé åã§ç®¡çã§ããç¹ã§ããããã«ãããã¡ã¢ãªæ¶è²»ãåæžããéããããªãœãŒã¹ç°å¢ã§ãå¹ççã«ããŒã¿ãæ±ãããšãå¯èœã§ããç¹ã«ãçµã¿èŸŒã¿ã·ã¹ãã ããããã¯ãŒã¯ããã°ã©ãã³ã°ãšãã£ãã¡ã¢ãªå¹çãéèŠãªåéã§å€ãçšããããŠããŸãããŸããç°ãªãããŒã¿åã«å¯ŸããŠåããã€ãåãå解éããç¹æ®ãªçšéã«ãé©ããŠããŸãã
ãªã¹ã¯ã®ç®¡çãšåã®å®å šæ§
å ±çšäœãå®å šã«äœ¿çšããããã«ã¯ãåã®å®å šæ§ãšã¡ã¢ãªãªãŒããŒã©ããã«å¯Ÿããç解ãéèŠã§ããã¿ã°ä»ãå ±çšäœãçšããŠçŸåšæå¹ãªããŒã¿åã管çããããšã§ãæå³ããªãåã®ã¢ã¯ã»ã¹ã«ããããŒã¿ç Žæãé²ãããšãå¯èœã§ãããŸããç°ãªãããŒã¿åã«ã¢ã¯ã»ã¹ããéã«ã¯ãå¿ ãã¡ã¢ãªã®ç¶æ ã䜿çšããŠããåã«æ³šæãæãå¿ èŠããããŸãã
å ±çšäœãå©çšããéã®å®çšçãªãã€ã³ã
- ã¿ã°ä»ãå ±çšäœã®äœ¿çšïŒã¿ã°ä»ãå ±çšäœãçšããããšã§ãçŸåšæå¹ãªã¡ã³ããŒãæ確ã«ç®¡çã§ããŸããããã«ãããåã®å®å šæ§ã確ä¿ãããããã°ã容æã«ãªããŸãã
- ãããã°ãšã³ã¡ã³ãã®å·¥å€«ïŒå ±çšäœã¯ãããã°ãé£ãããããã©ã®ã¡ã³ããŒãæåŸã«èšå®ãããããææ¡ããããããããã«ãã³ãŒãå ã«è©³çŽ°ãªã³ã¡ã³ããè¿œå ããããšãå¹æçã§ããããã¥ã¡ã³ãåãè¡ããä»ã®éçºè ã䜿çšæ¹æ³ãç解ããããããã«å·¥å€«ããŸãããã
- ãã©ãããã©ãŒã éã®äºææ§ã®ç¢ºèªïŒç°ãªããã©ãããã©ãŒã ã§å ±çšäœãæ±ãéã«ã¯ãç°å¢äŸåã®åœ±é¿ãåããå¯èœæ§ãããããããã¹ããè¡ãäºææ§ã確èªããããšã倧åã§ãã
å ±çšäœã®å®çšäŸãšãŸãšã
å ±çšäœã¯ãéãããã¡ã¢ãªç°å¢ã§ã®ããŒã¿ç®¡çã«åŒ·åãªããŒã«ãšãªããŸãããããã¯ãŒã¯ãã±ããã®è§£æãç°ãªãããŒã¿åã®å¹ççãªç®¡çãã¿ã°ä»ãå ±çšäœã®ãããªå·¥å€«ãéããŠãå ±çšäœã®ã¡ãªãããæ倧éã«åŒãåºãããšãã§ããŸãã