【发布时间】:2017-04-30 14:20:38
【问题描述】:
我正在编写一些代码,将字符串作为输入并将其反向返回。
当我输入一个字符串时,我得到一个“abort trap: 6”错误。我认为问题在于我对 strcpy 的使用(误用?),但 GDB 没有帮助,关于此错误和 strcpy 的其他问题也没有帮助我理解为什么会收到此错误。
我在代码中添加了一些 cmets 来解释预期的功能。
感谢您提供的任何帮助或阅读材料!
#include <stdio.h>
#include <string.h>
int main()
{
char line[1024];
fgets(line,sizeof(line),stdin);
int size = strlen(line);
for(int I = 0; I <size-I;I++)
{
char temp;
int relative_size = size-I;
strcpy(&temp,&line[I]);//??copies Ith character of line to temp??
strcpy(&line[I],&line[relative_size]); //??swaps characters at [I] and [size-I]??
strcpy(&line[relative_size],&temp);
}
printf("%s", line);
return 0;
}
【问题讨论】:
-
int relative_size = size-I;-->int relative_size = size-I-1;,strcpy(&temp,&line[I]);-->temp = line[I];等等。 -
@BLUEPIXY 完美运行!非常感谢。
标签: c string runtime-error strcpy