C語言一問

2006-11-05 10:30 am
struct 同 union 有咩分別?

回答 (1)

2006-11-05 7:34 pm
✔ 最佳答案
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


收錄日期: 2021-04-12 20:07:36
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061105000051KK00566

檢視 Wayback Machine 備份