C程序

时间:2015-12-23 17:33:57

标签: c gcc

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

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

4 个答案:

答案 0 :(得分:5)

您正在获得细分错误,因为您尚未为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);
}

答案 1 :(得分: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();
}

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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