※ 引述《curry.bbs@cis.nctu.edu.tw (綿延人生)》之銘言:
: sizeof(char * string)的用法
: 其理因是回傳字串的長度 , 但不函'\0'的ㄚ
: 可是好奇怪ㄌㄟ , 如下列程式碼的執行結果
: 1 #include<stdio.h>
: 2 #include<string.h>
: 3 main()
: 4 {
: 5 char test[5]="1232";
: 6
: 7
: 8 if(sizeof(test)==5)
: 9 printf("right\n");
: 10 else
: 11 printf("error\n");
: 12 }
: 卻會印出 right , 然到是系統版本的問題嗎 ?
: .>_<.
sizeof是根據你宣告的變數的型態來決定
char test[5] ==> sizeof會視為長度5的char 陣列
如果你用strlen的話..結果就不會是5..而是4
這根系統版本是沒有關係的...
--
有沒有哪麼一個地方
可以容納我的悲傷與哀愁 讓心也能有喘息的空間
有沒有那麼一個地方
可以收藏我的愁悵與無奈 讓心不再有牽掛的羈絆
我只想要一片藍天 任我自由敖翔
我只想要一處港口 讓心不再沉浮
--
※ Origin: 元智大學 風之塔 <bbs.yzu.edu.tw> ◆ From: [d1305c.dorm.yzu.edu]
> -------------------------------------------------------------------------- <
作者: Lapins (......) 看板: Programming
標題: Re: [ C ]sizeof的問題
時間: Sat Jan 2 13:19:08 1999
※ 引述《curry.bbs@cis.nctu.edu.tw (綿延人生)》之銘言:
: sizeof(char * string)的用法
: 其理因是回傳字串的長度 , 但不函'\0'的ㄚ
: 可是好奇怪ㄌㄟ , 如下列程式碼的執行結果
: 1 #include<stdio.h>
: 2 #include<string.h>
: 3 main()
: 4 {
: 5 char test[5]="1232";
: 6
: 7
: 8 if(sizeof(test)==5)
: 9 printf("right\n");
: 10 else
: 11 printf("error\n");
: 12 }
: 卻會印出 right , 然到是系統版本的問題嗎 ?
: .>_<.
你的sizeof其實是量測到 test這個buffer的長度
並非字串長度
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: Lapins.g1.ntu.e