【问题标题】:segmentation fault (core dumped) error in C programC 程序中的分段错误(核心转储)错误
【发布时间】:2015-12-23 17:33:56
【问题描述】:

我尝试使用 gcc linux 编译器编译并运行以下程序来反转字符串,但它显示错误:分段错误(核心转储)。我什至尝试使用 gdb 进行调试,但没有帮助。下面给出的程序首先输入 t,它是测试用例的数量。我用 3 个测试用例测试了程序,但是在接受用户的第二次输入后,编译器显示错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strrev(char*);

int main(int argc, char *argv[])
{
        int t,i=0,temp=0;
        char *str[10],*rev[10];
        scanf("%d",&t); //input the number of test cases
        while(i<t)
        {
            scanf("%s",str[i]);
            i++;
        }
        while(temp<t) //reverse the string and display it
        {
           rev[temp]=strrev(str[temp]);
           printf("%s \n",rev[temp]);
           temp++;
        }
    return 0;
    getchar();
}

反转字符串的功能:

char *strrev(char *str) 
{

    int i = strlen(str)-1,j=0;

    char ch;
    while(i>j)
    {
        ch = str[i];
        str[i]= str[j];
        str[j] = ch;
        i--;
        j++;
    }
    return str;
} 

【问题讨论】:

    标签: c gcc


    【解决方案1】:

    您遇到了分段错误,因为您没有为str 的元素分配空间。 您需要先在main 函数中分配内存。

    scanf("%d",&t); //input the number of test cases
    
    if(t <= 10)
        for(size_t i = 0; i < t; i++)
            str[i] = malloc(50); // Assuming string is no more than 50characters. 
    else  
        exit(0);  
    

    除此之外,您的代码中还有许多缺陷。这是修复它们后的代码

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void strrev(char*);  // Change return type to void
    
    int main(void)
    {
        int t,i=0,temp=0, ch;
        char *str[10];
        scanf("%d",&t); //input the number of test cases
        while((ch = getchar()) != EOF && ch != '\n'); // To consume newline character after scanf
    
        // Allocate memory for str elements
        if(t <= 10)
            for(size_t i = 0; i < t; i++)
                str[i] = malloc(50); // Assuming string is no more than 50characters.
        else
            exit(0);
    
        i = 0;
        while(i < t)
        {
            fgets(str[i],50,stdin); // Use fgets instead of scanf to read string
            i++;
        }
        while(temp<t) //reverse the string and display it
        {
            // Since you are reversing string by flipping the characters the same 
            // string just pass pointer to it. str[temp] will be updated in function.
            strrev(str[temp]);
            printf("Reverse is %s \n", str[temp]);
            temp++;
        }
        return 0;
    }
    
    void strrev(char *str)
    {
    
        size_t i = strlen(str)-1,j=0;
        char ch;
        while(i>j)
        {
            ch = str[i];
            str[i]= str[j];
            str[j] = ch;
            i--;
            j++;
        }
        //printf("Reverse is %s \n", str);
    }
    

    【讨论】:

      【解决方案2】:

      你错过了在读取 char* 值之前分配内存,所以你可以这样做:

      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>
      char* strrev(char*);
      
      int main(int argc, char *argv[])
      {
              int t,i=0,temp=0;
              char *str[10],*rev[10];
              scanf("%d",&t); //input the number of test cases
              while(i<t)
              {
                  str[i] = (char*)malloc(100); // just allocate memory
                  scanf("%s", str[i]);
                  i++;
              }
              while(temp<t) //reverse the string and display it
              {
                 rev[temp]=strrev(str[temp]);
                 printf("%s \n",rev[temp]);
                 temp++;
              }
          return 0;
          getchar();
      }
      

      【讨论】:

      • 好吧,我插入了内存分配行,但它没有用。我试图在 CLion 上运行这个程序,但它显示另一个错误,指出从 'void*' 到 'char*' 的无效转换。
      • @hacks 既然输出存储在 rev 数组中,则无需为其分配内存。
      • "从 'void*' 到 'char*' 的无效转换。"表示您正在使用 C++ 编译器。你需要使用 C 编译器,C 和 C++ 是不同的语言
      【解决方案3】:
      char *str[10],*rev[10];
      

      您尚未分配存储来保存这些指针的字符串值。

      char * str; /* this is a string pointer */
      
      char * str = malloc(15); /* this creates storage for a string */
      
      char str[10]; /* this creates a static char array, also is a string */
      

      【讨论】:

        【解决方案4】:

        我也有同样的问题。我只是通过更正矩阵的索引来修复它。

        【讨论】:

          猜你喜欢
          • 2019-01-14
          • 2021-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-14
          • 2017-02-25
          • 2016-07-12
          相关资源
          最近更新 更多