几个有意思的基础C题,结果需要深思
- [root@ethan lua]# cat testsh.cpp
- #include<iostream>
- using namespace std;
- union t
- {
- int i;
- char str[2];
- };
- union myun
- {
- struct {int x; int y; int z;} u;
- int k;
- int j;
- }a;
- int main()
- {
- t test;
- test.i=0;
- test.str[0]=20;
- test.str[1]=1;
- // cout <<"str[0]=" << test.str[0] << " str[1]=" << test.str[1] << endl;
- cout << "test.i = " << test.i << endl;
- a.u.x = 4;
- a.u.y = 5;
- a.u.z = 6;
- a.k = 0;
- cout << a.u.x << " " << a.u.y << " " << a.u.z << endl;
- a.j = 1;
- cout << a.u.x << " " << a.u.y << " " << a.u.z << endl;
- char a[] = "abc";
- char *pa = "abc";
- char b[] = "abc";
- //char *pa = "abc";
- char *pb = "abc";
- const char c[] = "abc";
- const char* ppa = "abc";
- const char d[] = "abc";
- //const char* ppa = "abc";
- const char* ppb = "abc";
- cout << (a == b) << endl;
- cout << (pa == pb) << endl;
- cout << (c == d) << endl;
- cout << (ppa == ppb) << endl;
- return 0;
- }
[root@ethan lua]# ./testsh
test.i = 276 //字符间数据是相加的。。?
0 5 6
1 5 6 //联合体永远替换同空间
0
1
0
1
阅读(759) | 评论(0) | 转发(0) |