【发布时间】:2017-05-03 01:12:28
【问题描述】:
我在用 C 编写程序时遇到了一个奇怪的错误。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
int main (int argc, char* argv[])
{
int boardSize = atoi(argv[2]);
int generations = atoi(argv[4]);
int gamesort = atoi(argv[1]);
printf("2 is %d 1 is %d 4 is %d name of file is %s \n",boardSize,gamesort,generations,argv[3]);
if (1==1)
{
printf("yes");
ZeroPlayersGame(boardSize, generations,argv[3]);
}
else//(gamesort==2)
{
TwoPlayersGame(boardSize, generations,argv[3]);
}
return 0;
}
这是我从终端得到的错误:
ise@ubuntu:~/Desktop$ make
gcc -c main.c defs.c gameIO.c zeroPlayer.c twoPlayer.c
gcc gameIO.o defs.o zeroPlayer.o main.o twoPlayer.o -o prog
ise@ubuntu:~/Desktop$ ./prog 1 2 "l.txt" 3
2 is 2 1 is 1 4 is 3 name of file is l.txt
Segmentation fault (core dumped)
很奇怪,你可以看到我的程序没有输入我的第一个“if”, 但你可以看到它打印了 if 语句之前的行。
感谢您的帮助!
【问题讨论】:
-
你怎么知道的?你没有冲水。
-
如果打印出来的话,我的里面有一个 printf。
-
为了确保输出被刷新到终端,尝试添加一个换行符:
printf("yes\n");。您在致电ZeroPlayersGame()时遇到了崩溃 -
如果您删除函数调用(
ZeroPlayersGame和TwoPlayersGame)行?你还在遇到分段错误吗? -
@YardenRotem 太棒了!祝你好运;-)
标签: c if-statement gcc operators