简体   繁体   English

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

[英]segmentation fault (core dumped) error in C program

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. 我尝试使用Linux的gcc编译器编译并运行以下程序来反转字符串,但显示错误:segmentation fault(core dumped)。我什至尝试使用gdb进行调试,但无济于事。 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. 下面给出的程序首先输入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;
} 

You are getting segmentation fault because you haven't allocated space for elements of str . 由于没有为str元素分配空间,因此出现了分段错误。 You need to allocate memory first in main function. 您需要首先在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);
}

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();
}
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 */

I also had the same issue. 我也有同样的问题。 I just fixed it by correcting the indices of the matrix. 我只是通过校正矩阵的索引来修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM