C/C++ union 用法與範例

本篇 ShengYu 介紹 C/C++ union 用法與範例,union 是一種將不同資料類型 data type 儲存在同一塊記憶體空間的型別,所有在 union 裡宣告的變數會共享同一塊記憶體空間,union 佔用的記憶體空間會以 union 內宣告的變數類型最大 size 的變數空間。

  • C/C++ union 基本用法
  • C/C++ union 初始化
  • C/C++ typedef union 取別名
  • C/C++ 匿名 union

C/C++ union 基本用法

C/C++ union 只會選擇一種變數類型儲存,且會以變數類型最大 size 的變數空間作為 union 佔用的記憶體空間,如下範例,
student union 中的 id 與 name 一次只能選擇使用一種存取,不能同時使用兩者,因為 id 與 name 佔的是同一塊記憶體空間,

cpp-union.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// g++ cpp-union.cpp -o a.out
#include <stdio.h>
#include <string.h>

union student {
int id;
char name[32];
};

int main() {
printf("int size: %d\n", sizeof(int));

union student s1;
s1.id = 1;
printf("union size: %d\n", sizeof(s1));

union student s2;
strcpy(s2.name, "shengyu");
printf("union size: %d\n", sizeof(s2));

return 0;
}

輸出如下,

1
2
3
int size: 4
union size: 32
union size: 32

C/C++ union 初始化

這邊介紹 C/C++ union 初始化的幾種寫法,如下範例所示,

cpp-union2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// g++ cpp-union2.cpp -o a.out
#include <stdio.h>

union data {
char c;
int i;
float f;
};

int main() {
union data d1;
d1.c = 'a';
printf("%c\n", d1.c);

union data d2 = { 'b' };
printf("%c\n", d2.c);

union data d3 = d2;
printf("%c\n", d3.c);

return 0;
}

輸出如下,

1
2
3
a
b
b

C/C++ typedef union 取別名

每次宣告變數時都必須加上 union,如果想要省略每次宣告變數前的 union,可以利用 typedef 取別名的方式,如下範例,使用 typedef 將 union student 取一個 Student 新別名後,之後宣告時就可以使用新的 Student 別名,就可以省去加上 union,藉此達到簡化宣告語法,

cpp-union3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// g++ cpp-union3.cpp -o a.out
#include <stdio.h>
#include <string.h>

typedef union student {
int id;
char name[16];
} Student;

int main() {
Student s1;
s1.id = 1;
printf("union size: %d\n", sizeof(s1));

Student s2;
strcpy(s2.name, "shengyu");
printf("union size: %d\n", sizeof(s2));

return 0;
}

另外還有另外一種寫法,可以把 union 的定義跟 typedef 分開寫,typedef 最後面記得要加上分號,

1
2
3
4
5
6
union student {
int id;
char name[32];
};

typedef union student Student;

C/C++ 匿名 union

這邊介紹一下 C/C++ 匿名 union(anonymous union),先從一般的 union 開始說明,一般的 union 可以放在 struct 裡,如下範例所示,

cpp-union4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// g++ cpp-union4.cpp -o a.out
#include <stdio.h>

union data {
int i;
char c;
};

struct student {
int id;
union data d;
};

int main() {
struct student s;
s.id = 1;
s.d.c = 2;
printf("%d\n", s.d.c);

return 0;
}

也可以這樣寫,直接將 data union 寫在 student struct 裡,

cpp-union5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// g++ cpp-union5.cpp -o a.out
#include <stdio.h>

struct student {
int id;
union {
int i;
char c;
} data;
};

int main() {
struct student s;
s.id = 1;
s.data.c = 2;
printf("%d\n", s.data.c);

return 0;
}

如果拿掉 data union 的名稱的話,這樣寫的話就是匿名 union(anonymous union),如下範例所示,差別在存取匿名 union 可以少寫一層,直接使用 union 中元素的名稱來存取該元素,少了在前面 union 的名稱,少一點程式碼,

cpp-union6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// g++ cpp-union6.cpp -o a.out
#include <stdio.h>

struct student {
int id;
union {
int i;
char c;
};
};

int main() {
struct student s;
s.id = 1;
s.c = 2;
printf("%d\n", s.c);

return 0;
}

以上就是 C/C++ union 的用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 C/C++ 相關技術,可以參考看看下面的文章,
C/C++ 新手入門教學懶人包
C/C++ enum 用法與範例
C/C++ struct 用法與範例