C语言 C程序中的分段错误(核心转储)错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34440921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:16:55  来源:igfitidea点击:

segmentation fault (core dumped) error in C program

cgcc

提问by Shreeya Kharbanda

I tried to compile and run the following program to reverse a string using the gcc compiler for linux but it shows the error : segmentation fault (core dumped).I even tried to debug using gdb but it didn't help. The program given below firstly inputs t which is the number of test cases.I tested the program with 3 test cases but after taking the 2nd input from user, the compiler shows error.

我尝试编译并运行以下程序以使用 linux 的 gcc 编译器反转字符串,但它显示错误:分段错误(核心转储)。我什至尝试使用 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();
}

Function to reverse the string:

反转字符串的函数:

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;
} 

回答by haccks

You are getting segmentation fault because you haven't allocated space for elements of str. You need to allocate memory first in mainfunction.

您遇到分段错误,因为您尚未为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);  

Beside this there are many flaws in your code. Here is the code after fixing them

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

#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);
}

回答by developer

you have missed to allocate memory before reading char* value, so you can do this:

您在读取 ​​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();
}

回答by mrflash818

char *str[10],*rev[10];

You did not assign storage to hold string values yet for those pointers.

您还没有为这些指针分配存储来保存字符串值。

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 */

回答by mgokhanbakal

I also had the same issue. I just fixed it by correcting the indices of the matrix.

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