Skip to content

Instantly share code, notes, and snippets.

@relax-more
Last active December 3, 2015 06:05
Show Gist options
  • Save relax-more/062833dcf878cd842211 to your computer and use it in GitHub Desktop.
Save relax-more/062833dcf878cd842211 to your computer and use it in GitHub Desktop.
共用体使ったこと無いけど便利そう
#include <stdio.h>
#include <string.h>
union U /* 共用体Uを定義 */
{
double x;
int y;
char z[10]; /* int,double,char[10]のいずれかを格納できる */
};
union Hoge /* 共用体Hogeを定義:構造体の共用体 */
{
struct Foo{
int ifoo;
double dfoo;
} foo; /* 構造体Fooとメンバfooの定義 */
struct Bar{
int ibar;
void *pbar;
} bar; /* 構造体Barとメンバbarの定義 */
}; /* FooとBarの先頭メンバは同じ型(int) */
int main(void)/* 共用体を使ってみる */
{
union U u = { 100.0 }; /* 共用体変数の宣言と初期化 */
/* 先頭メンバの型(double)で初期化しなければならない */
union Hoge hoge;
struct Foo f = { 3, 0.14 }; /* ※注:C++では「Hoge::Foo f = ...;」とする */
u.y = 42; /* int型の値を書き込む */
strcpy(u.z, "UnionTest"); /* charの配列を書き込む */
/* u.y += 100; */ /* 不正:charの配列が入ったuをint型として扱っている */
hoge.foo = f; /* Foo構造体を書き込む */
hoge.bar.ibar = 9; /* OK:Barの先頭メンバとしてアクセスしても良い */
/* hoge.bar.pbar = NULL; */ /* これはダメ:今hogeに入っているのはあくまでFoo型 */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment