JonsenElizee

Software Developing Blog

"An idea is fragile . It can be killed by a scornful smile or a yawn .It can be mound down by irony and scared to death by a cold look."
"Most cultures throughout human history have not liked creative individuals .They ignore them or kill them.It is a very efficient way of stopping creativity."

------Advertising boss Charles Browe and Howard Gardner ,professor at Harvard

   :: 首页 :: 新随笔 ::  ::  :: 管理 ::
Demo Code:


  1 #include <stdio.h>
  2 union my_union {
  3         char c1[3];
  4         // ERROR: a brace-enclosed initializer
  5         // is not allowed here before ‘{’ token
  6         //char c1[] ={'a'};
  7         char c2[6];
  8         // ERROR: ISO C++ forbids initialization of member ‘i
  9         // int i = 0;
 10 };
 11
 12 union his_union {
 13         union my_union u;
 14         int i;
 15 };
 16
 17 void doit(union my_union &u) {
 18         u.c1[0] = 'A';
 19 }
 20
 21 union my_union get() {
 22         union my_union u;
 23         u.c1[0] = 'X';
 24         return u;
 25 }
 26
 27 int main(int argc, char* argv[]) {
 28         int s = sizeof(union my_union);
 29         printf("s == %d\n", s); // 6
 30         s = sizeof(union his_union);
 31         printf("s == %d\n", s); // 8, for the int i, 6+2=8.
 32         union my_union u;
 33         u.c1[0] = 'a';
 34         doit(u);
 35         printf("%c\n", u.c1[0]); // A
 36         u = get();
 37         printf("%c\n", u.c1[0]); // X
 38         return 0;
 39 }
"xxx.cpp" 39L, 691C written 

Output:
[root@localhost tmp]# g++ xxx.cpp && a.out
s == 6
s == 8
A
X
[root@localhost tmp]#


posted on 2010-09-12 22:53 JonsenElizee 阅读(314) 评论(0)  编辑 收藏 引用
By JonsenElizee