#include<stdio.h>
#include<string.h>
int main(void) {
int t, n, i, j;
char dir[41][52];
char right[] = "Right";
char left[] = "Left";
char *p = NULL, storeDir[10], *k = NULL;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
fflush(stdin);
for(i = 0; i < n; i++) {
gets(dir[i]);
fflush(stdin);
}
i = n - 1;
k = dir[i];
j = 0;
p = strchr(dir[i], 'o');
while(*k != ' ') {
storeDir[j] = *k;
k++;
j++;
}
storeDir[j] = '\0';
printf("Begin ");
printf("%s\n", p);
for(i = n - 2; i >= 0; i--) {
if(strcmp(storeDir, right) == 0) {
printf("Left ");
}
else if(strcmp(storeDir, left) == 0) {
printf("Right ");
}
k = dir[i];
j = 0;
p = strchr(dir[i], 'o');
while(*k != ' ') {
storeDir[j] = *k;
k++;
j++;
}
storeDir[j] = '\0';
printf("%s\n", p);
}
printf("\n");
}
return 0;
}
这是一个代码,它只接收一些字符串并通过使用指针操作它们来打印出来。它在我的计算机上运行良好,但是当我在 http://ideone.com/上运行它时,它会出现 SIGSEGV 错误。我认为这是由于一些无效的指针引用。但是,我无法弄清楚我哪里出错了。有人可以向我指出这一点吗?这是该程序的示例测试用例:
4
Begin on Road A
Right on Road B
Right on Road C
Left on Road D
TIA