有關C語言小程式問題

2009-07-18 5:08 pm
我寫了個小程式
有compile過,
可是執行時一直"當"掉= =
想請幫忙看是哪裡有寫錯.

我是想寫
可以由使用者自行輸入一任意字串
然後再由使用者任意輸入一字元
執行完之後
若字串裡面有使用找想找的字元,就顯示找到某字元
若字串裡面沒有使用者想找到字元就顯示沒找到.

下面是我寫的code
#include <stdio.h>
#include <string.h>

int main(){
char word[21];
char w=0;
char* pget=NULL;


printf("Enter string:");
scanf("%s",word);


printf("Which character do you want to search for?");
scanf("%c",&w);


pget=strchr(word,w);

if(pget!=NULL)
printf("Character %c was found.",*pget);
else
printf("Sorry,the string you enter has no the character %c",*pget);

system("pause");
return 0;
}
更新1:

pget=strchr(word,w);可我有寫這一行?

回答 (2)

2009-07-18 5:57 pm
✔ 最佳答案
printf("Sorry,the string you enter has no the character %c",*pget);

pget = NULL , 卻要顯示 *pget 所以當機

2009-07-23 20:51:07 補充:
A pointer to the first occurrence of character in str.
If the value is not found, the function returns a null pointer.

以上是strchr的回傳值解說,也就是說當你在字串裡面找不到指定的字元時,他回傳的還是NULL,所以pget = NULL...當機
2009-07-18 5:46 pm
The following line:
printf("Sorry,the string you enter has no the character %c",*pget);

Value of pget is NULL and thus *pget will cause an error.


收錄日期: 2021-04-30 13:01:44
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090718000016KK02135

檢視 Wayback Machine 備份