![](/img/trans.png)
[英]Why is the CodeBlocks compiler not recognizing a function that I declared in a header file in C?
[英]When I was trying to run this program in the codeblocks compiler, the strstr() function was returning 0 everytime. Why?
我正在編寫一個程序,通過從用戶獲取輸入字符串來從軌道列表中搜索軌道。
#include<stdio.h>
#include<string.h>
char tracks[][80] = {
"I left my heart in harvard med school",
"Newark, newark - a wonderful town",
"Dancing with a dork",
"From here to maternity",
"The girl from Iwo Jima",
};
void find_track(char search_for[])
{
int i, m = 0;
for(i = 0; i<5; i++)
{
控件達到這一行代碼
m = strstr(tracks[i], search_for);
if(m)
{
printf("Track%i : '%s' \n", i, tracks[i]);
}
}
}
strstr()返回0
int main()
{
char search_for[80];
printf("Search for : ");
fflush(stdin);
fgets(search_for, 80, stdin);
find_track(search_for);
return 0 ;
}
請注意, fgets
包含該條目中包含的所有尾隨newline
。 可以這樣刪除
search_for [ strcspn(search_for, "\r\n") ] = 0; // remove trailing newline etc
否則您將找不到匹配項。
注意fflush(stdin);
是非標准的。 還要注意,在一個鏈接的答案中給定的scanf
在第一個空格處停止,除非您使用某種格式來阻止它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.