有時(shí)候錯(cuò)誤的代碼會(huì)讓我們更深刻的理解一門(mén)編程語(yǔ)言,所以在Learn c the hard way一書(shū)中,作者不停的讓我們把正確的代碼破壞掉,發(fā)揮我們的好奇心與想象力,去發(fā)覺(jué)c語(yǔ)言的一些特性。
為了弄清弄清c中int與char型變量存儲(chǔ)的異同和gcc對(duì)無(wú)'\0'字符數(shù)組的的反應(yīng),修改了Learn c the hard way書(shū)中的ex8
代碼如下
#include <stdio.h>
int main(int argc,char *argv[])
{
int areas[] = {10, 12, 13, 14, 20};
char name [] = "Zed";
char full_name[] = {
'Z', 'e', 'd',
' ', 'A', '.', ' ',
'S', 'h', 'a', 'w'
};
unsigned int interg[2];
interg[0] = '\0' * 0x01000000 + 'A'*0x00010000 + 'B' * 0x00000100 + 'C' * 0x00000001;
unsigned int inter = interg[0];
// WARNING: On some system you may have to change the
// %ld in this code to a %u since it will use unsignt ints
printf("The size of an int: %ld\n",sizeof(int));
printf("The siez of areas (int[]):%ld\n",
sizeof(areas));
printf("The number of ints in areas: %ld\n",
sizeof(areas)/sizeof(int));
printf("The first area is %d, the 2nd %d.\n",
areas[0],areas[10]);
printf("The size of a char: %ld\n",sizeof(char));
printf("The size of name (char[]): %ld\n",
sizeof(name)/sizeof(char));
printf("The size of full_name (char[]): %ld\n",
sizeof(full_name));
printf("The number of chars: %ld\n",
sizeof(full_name) / sizeof(char));
// !!!其中full_name未設(shè)置null結(jié)尾
printf("name=\"%s\" and full_name=\"%s\"\n",
name, full_name);
// test 證明了gcc把未初始化的地方初始為0
printf("the char after the full_name(shuold be error):%c\n",full_name[12]);
// 用 int 存儲(chǔ)字符串
printf("interg=%X\n", interg);
printf("inter=\"%s\"\n", &inter);
printf("interg=\"%s\"\n", interg);
return 0;
}
重點(diǎn)在于
char full_name[] = {
'Z', 'e', 'd',
' ', 'A', '.', ' ',
'S', 'h', 'a', 'w'
};
unsigned int interg[2];
interg[0] = '\0' * 0x01000000 + 'A'*0x00010000 + 'B' * 0x00000100 + 'C' * 0x00000001;
unsigned int inter = interg[0];
可以看到full_name字符串初始化的時(shí)候是沒(méi)有null結(jié)尾的,如果的vc中編譯并以字符串形式輸出的話會(huì)跟上亂碼
interg的數(shù)組第一個(gè)元素中我分別在四個(gè)字節(jié)中放入了'\0'(即0)、'A'、'B'、'C',第二個(gè)元素未初始化。
inter中放的是同樣的數(shù)字。
輸出如下:
The size of an int: 4
The siez of areas (int[]):20
The number of ints in areas: 5
The first area is 10, the 2nd -1493946707.
The size of a char: 1
The size of name (char[]): 4
The size of full_name (char[]): 11
The number of chars: 11
name="Zed" and full_name="Zed A. Shaw"
the char after the full_name(shuold be error):
interg=6EB11E70
inter="CBA"
interg="CBA"
可以看到以字符串輸出的full_name并沒(méi)有因?yàn)闊o(wú)null結(jié)尾而輸出亂碼或錯(cuò)誤,在csdn論壇上了解到可能是因?yàn)間cc將字符串存儲(chǔ)區(qū)全部初始化為零。
PS:對(duì)于數(shù)組的初始化,可以只初始化一個(gè)元素,就可以使其他元素為零(Learn c the hard way);
inter以字符串輸出的結(jié)果是按我賦值的倒序輸出的,這個(gè)是因?yàn)樾《说臄?shù)據(jù)存儲(chǔ)造成的托福答案