"*程序收到信号 SIGSEGV,分段错误
。concat 中的 0x000000000040073b (nextstr=0x7fffffffee66 "the",
longstr=0x7fffffffe750 "", i=0x0) at main.c:24
24 printf("%c -> %c\n" , nextstr[j], longstr[*i]);
(gdb)"
// define max for command line
#define MAX_CHARS 1000
#include <stdio.h>
int concat(char[], char[], int *);
void printreverse(char[], int);
int main(int argc, char *argv[])
{
char longstring[MAX_CHARS] = { '\0' };
int i = 0, j;
if (argc < 2)
{
printf("%s requires command-line args\n", argv[0]);
return 1;
}
for (j = 1; j <= argc; j++)
{
if (concat(argv[j], longstring, i))
return 1;
}
i--;
longstring[i] = '\0'; //delete trailing space
printf("%s\n", longstring);
}
int concat(char nextstr[], char longstr[], int *i)
{
int j = 0;
while (nextstr[j] != '\0')
{
printf("%c -> %c\n", nextstr[j], longstr[*i]);
longstr[*i] = nextstr[j];
(*i)++;
j++;
if (*i > MAX_CHARS)
{
printf("Error: Input is too long!\n");
return 1;
}
}
if (j > 0)
{
if ((*i) + 2 > MAX_CHARS)
{
printf("Error: Input is too long!\n");
return 1;
}
longstr[*i] = ' ';
longstr[(*i) + 1] = '\0';
(*i)++;
}
return 0;
}
指针错误最有可能
“这需要一个命令行参数是检查长度,但有一个段错误”
我认为错误在第 33 行,但我不知道如何解决它