关于《Head First C》上面的一道例题的疑问
最近在学习Head First C这本书,看到strstr()那部分的例题,一道在字符串数组里面查找字符串的题目,上机实现了一下,发现有问题,程序没报错,但没有正确的运行。无论如何也找不到出现问题的原因,希望大家能帮忙一下。
- C/C++ code
#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; for (i = 0; i < 5; i++) { if (strstr(tracks[i], search_for)) printf("Track %i: '%s'\n", i, tracks[i]); }}int main(){ char search_for[80]; printf("Search for: "); fgets(search_for, 80, stdin); find_track(search_for); return 0;}我断点跟踪了一下strstr(tracks[i], search_for)这个表达式的结果,发现居然无返回值,然后我在循环里设置了一个临时量char *tmp存储strstr()的返回值,发现也不行。难道strstr()返回的不是指针吗?谁能帮忙看下问题出在哪里了?
PS.这个是原书上的代码,而且书上居然能正常运行。我这边在VC6,C Free和GCC下都不能正常运行。
[解决办法]
算了,复制一下msdn吧
- C/C++ code
The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string. fgetws is a wide-character version of fgets. fgetws reads the wide-character argument string as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.