我正在编写一个小程序来检查函数strcasestr是如何工作的。
下面的代码做了什么:
- 要求用户输入一行文本。 
 第一个输入- blah bla blah blah the Word we are looking for.示例: 第二个输入示例:- Word
- 程序应该打印的是: - Word we are looking for.
- 但它给了我一个分段错误(核心转储)错误。 
我怀疑我使用fgets()不正确。当我运行程序scanf用于读取输入(当然输入第一个不带空格的输入)时,它可以工作并给我预期的输出。
知道是什么导致了分段错误错误吗?如何纠正这个?
#define _GNU_SOURCE
#define max_buff 1000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *answer;
//char * strcasestr (const char *haystack, const char *needle);
void strip_crlf(char* s);
void strip_crlf(char* s)
{
    char* p = strpbrk(s, "\r\n");
    if (p) *p = '\0';
}
int main(){
  char fname[max_buff+1];
  char lname[max_buff+1];
  printf("Enter the line of text you are searching for ");
  //scanf("%s", fname);
  fgets(fname,max_buff,stdin);
  //printf("%s",fname);
  strip_crlf(fname);
  printf("Enter the search term ");
  //scanf("%s", lname);
  fgets(lname,max_buff,stdin);
  strip_crlf(lname);
  //printf("%s",lname);
  if((answer=strcasestr(fname,lname))!=NULL){
  // printf("now we have something in answer ");
    printf("%s\n",answer);
  }
  else
      printf(" strcasestr failed\n");
}
已编辑:反映以下评论/答案中提出的建议。该程序现在打印:
 strcasestr failed
...呃。
Edit2:程序现在可以工作了。感谢大家的帮助!