English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

struct 同 union 有咩分別?

2006-11-04 21:30:33 · 1 個解答 · 發問者 牧場物語迷 6 in 電腦與網際網路 程式設計

1 個解答

They both have similar syntax, but the memory allocations are different.
Struct variables add up, and do not share.
Union variables share the same memory, so one can destroy another. We do this to save memory space.
An example:
union a1(
int x;
char y;
double z;
)u1;
u1.x=4;
u1.y='a';
printf("%d",u1.x)
You will NOT get 4, because y has destroyed the value, since x,y and z share the same memory space. The space occupied by union is the greatest of them, namely double in this case (8 bytes).
I

2006-11-05 06:34:05 · answer #1 · answered by p 6 · 0 0

fedest.com, questions and answers