C “什么是”呢;分段故障(堆芯倾倒)“;为什么它会在我的输出中返回?

C “什么是”呢;分段故障(堆芯倾倒)“;为什么它会在我的输出中返回?,c,fgets,getchar,strlen,C,Fgets,Getchar,Strlen,我试图用一个对fgets的调用来替换包含重复getchar调用的循环 当我尝试键入输入时,我得到了分段错误(核心转储),我不知道这是什么,也不知道为什么会得到它 启动器代码 /* Example: analysis of text */ #include <stdio.h> #include <string.h> #define MAX 1000 /* The maximum number of characters in a line of input */ ma

我试图用一个对fgets的调用来替换包含重复getchar调用的循环

当我尝试键入输入时,我得到了分段错误(核心转储),我不知道这是什么,也不知道为什么会得到它

启动器代码

/* Example: analysis of text */

#include <stdio.h>
#include <string.h>

#define MAX 1000 /* The maximum number of characters in a line of input */

main()
{
  char text[MAX], c;
  int i;
  int lowercase, uppercase, digits, other;
  int length;

  puts("Type some text (then ENTER):");

  /* Save typed characters in text[]: */
  // In ex1.c, please implement the following loop with fgets() and use strlen() to compute the length of the string
  //
  for (i = 0; i < MAX; i++)
  {
    text[i] = getchar();
    if (text[i] == '\n')
      break;
  }
  length = i;

  /* Analyse contents of text[]: */

  for (i = lowercase = uppercase = digits = other = 0; i < MAX; i++)
  {
    c = text[i];
    if (c >= 'a' && c <= 'z')
      lowercase++;
    else if (c >= 'A' && c <= 'Z')
      uppercase++;
    else if (c >= '0' && c <= '9')
      digits++;
    else
    {
      if (c == '\n')
        break;
      other++;
    }
  }

  puts("\nYou typed:");
  printf("A string with %d characters\n", length);
  printf("\t%d lower case letters\n", lowercase);
  printf("\t%d upper case letters\n", uppercase);
  printf("\t%d digits\n", digits);
  printf("\t%d others\n", other);
}
/* Example:  analysis of text */
#include <stdio.h>
#include <string.h>
#define MAX 1000 /* The maximum number of characters in a line of input */

main()
{
  char text[MAX], c;
  int i;
  int lowercase, uppercase, digits, other;
  int length;

  puts("Type some text (then ENTER):");

  /* Save typed characters in text[]: */
  // In ex1.c, please implement the following loop with fgets() and use strlen() to compute the length of the string
  //
  c = fgets(text, MAX, stdin);
  length = strlen(c);

  /* Analyse contents of text[]: */

  for (i = lowercase = uppercase = digits = other = 0; i < MAX; i++)
  {
    c = text[i];
    if (c >= 'a' && c <= 'z')
      lowercase++;
    else if (c >= 'A' && c <= 'Z')
      uppercase++;
    else if (c >= '0' && c <= '9')
      digits++;
    else
    {
      if (c == '\n')
        break;
      other++;
    }
  }

  puts("\nYou typed:");
  printf("A string with %d characters\n", length);
  printf("\t%d lower case letters\n", lowercase);
  printf("\t%d upper case letters\n", uppercase);
  printf("\t%d digits\n", digits);
  printf("\t%d others\n", other);
}
我的代码

/* Example: analysis of text */

#include <stdio.h>
#include <string.h>

#define MAX 1000 /* The maximum number of characters in a line of input */

main()
{
  char text[MAX], c;
  int i;
  int lowercase, uppercase, digits, other;
  int length;

  puts("Type some text (then ENTER):");

  /* Save typed characters in text[]: */
  // In ex1.c, please implement the following loop with fgets() and use strlen() to compute the length of the string
  //
  for (i = 0; i < MAX; i++)
  {
    text[i] = getchar();
    if (text[i] == '\n')
      break;
  }
  length = i;

  /* Analyse contents of text[]: */

  for (i = lowercase = uppercase = digits = other = 0; i < MAX; i++)
  {
    c = text[i];
    if (c >= 'a' && c <= 'z')
      lowercase++;
    else if (c >= 'A' && c <= 'Z')
      uppercase++;
    else if (c >= '0' && c <= '9')
      digits++;
    else
    {
      if (c == '\n')
        break;
      other++;
    }
  }

  puts("\nYou typed:");
  printf("A string with %d characters\n", length);
  printf("\t%d lower case letters\n", lowercase);
  printf("\t%d upper case letters\n", uppercase);
  printf("\t%d digits\n", digits);
  printf("\t%d others\n", other);
}
/* Example:  analysis of text */
#include <stdio.h>
#include <string.h>
#define MAX 1000 /* The maximum number of characters in a line of input */

main()
{
  char text[MAX], c;
  int i;
  int lowercase, uppercase, digits, other;
  int length;

  puts("Type some text (then ENTER):");

  /* Save typed characters in text[]: */
  // In ex1.c, please implement the following loop with fgets() and use strlen() to compute the length of the string
  //
  c = fgets(text, MAX, stdin);
  length = strlen(c);

  /* Analyse contents of text[]: */

  for (i = lowercase = uppercase = digits = other = 0; i < MAX; i++)
  {
    c = text[i];
    if (c >= 'a' && c <= 'z')
      lowercase++;
    else if (c >= 'A' && c <= 'Z')
      uppercase++;
    else if (c >= '0' && c <= '9')
      digits++;
    else
    {
      if (c == '\n')
        break;
      other++;
    }
  }

  puts("\nYou typed:");
  printf("A string with %d characters\n", length);
  printf("\t%d lower case letters\n", lowercase);
  printf("\t%d upper case letters\n", uppercase);
  printf("\t%d digits\n", digits);
  printf("\t%d others\n", other);
}
非常感谢您的帮助。
我也是C语言的新手,所以请尽可能简单地解释一下


改变
长度=strlen(c)
length=strlen(文本)修复了它。谢谢大家!

您的错误或至少其中一个错误出现在以下行中:

char text[MAX], c;
// ...

c = fgets(text, MAX, stdin);
length = strlen(c);
fgets
的返回值是指向
字符的指针,但您将其存储在
字符中,然后尝试将
字符
值传递给需要指向字符
指针的函数。由于
char
只有8位宽(在您要编译的任何机器上),指针需要32或64位,因此大部分位丢失,结果是指针无效。如果幸运的话,这将导致程序因分段错误而崩溃

这段代码实际上根本不应该编译。如果您至少没有收到一条警告,说明
c
无法保存指向
char
指针,则需要启用更多警告标志。(在
gcc
clang
上,我通常使用
-std=c99-Wall-Wextra-Wpedantic-Wconversion
进行编译)然后,至少在学习该语言时,将收到的任何警告视为程序中的错误。更好的是,添加
-Werror
(或编译器的等效项),使编译器以这种方式处理它们

最简单的解决方法是消除
c
而改为写

fgets( text, MAX, stdin );
length = strlen(text);

问题应该是独立的(参见)。不要发布代码链接,也不要发布代码图像。这意味着您的程序正在尝试读取或写入一个不存在的内存位置。您的代码甚至不应该编译
char
char*
是两种截然不同的类型。打开所有编译器警告,永远不要忽略它们
文本将被
\0
而不是
\n
终止@Stavr00
fgets()
读取一行并在其中保留换行符,除非该行长于
MAX