【发布时间】: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;
}
【问题讨论】: