[英]How to fix segmentation fault in this code?
如果这太愚蠢了,我感到非常抱歉,但是我只是从c开始并进行一般的编码。 我也很抱歉这是西班牙语,希望它不要太混乱。 我正在尝试使用矩阵对棋盘游戏进行编码,这时我只想能够打印棋盘。 我尝试使用Eclipse,但不断出现诸如command-gdb版本错误之类的错误。 我还尝试在联机C编译器上运行它,但没有成功(在这种情况下,我得到了分段错误)。
#include <stdio.h>
#include <stdlib.h>
char *tablero[9][9];
char Rey = 'R';
char gralOro = 'O';
char gralPlata = 'P';
char caballos = 'C';
char lanceros = 'L';
char alfil = 'A';
char torre = 'T';
char peones = 'p';
char vacio = ' ';
char *piezasNegrasPerdidas[20];
char *piezasBlancasPerdidas[20];
void initBoard(){
for(int i = 0; i<0; i++){
for(int j = 0; j<9; j++){
if(i==2 || i==6){
tablero[i][j] = &peones;
}
else if(i==3 || i==4 || i==5){
tablero[i][j]=&vacio;
}
}
}
tablero[0][0] = &lanceros;
tablero[0][1] = &caballos;
tablero[0][2] = &gralPlata;
tablero[0][3] = &gralOro;
tablero[0][4] = &Rey;
tablero[0][5] = &gralOro;
tablero[0][6] = &gralPlata;
tablero[0][7] = &caballos;
tablero[0][8] = &lanceros;
tablero[1][1] = &alfil;
tablero[1][7] = &torre;
tablero[8][0] = &lanceros;
tablero[8][1] = &caballos;
tablero[8][2] = &gralPlata;
tablero[8][3] = &gralOro;
tablero[8][4] = &Rey;
tablero[8][5] = &gralOro;
tablero[8][6] = &gralPlata;
tablero[8][7] = &caballos;
tablero[8][8] = &lanceros;
tablero[7][1] = &alfil;
tablero[7][7] = &torre;
}
void printTablero(){
for (int r = 0; r < 9; r++){
for(int c = 0; c<9; c++){
printf("[%c]", *(tablero[r][c]));
}
printf("\n");
}
}
int main(){
printTablero();
return 0;
}
我希望能够运行此代码,但是即使进行了一些研究,我对于可能出什么问题还是一无所知。
尽管可以通过指向char的2D指针数组以这种方式完成操作,但我不建议这样做。 相反,只需具有2D字符数组:
char tablero[9][9]; // without the *
然后像这样更改初始化:
tablero[0][0] = lanceros; // without the &
并打印到此:
printf("[%c]", tablero[r][c]);
同样,您的for (int i = 0; i < 0; i++)
循环永远不会运行。 对其进行更改,以正确地初始化所有字段。 我假设您想要一个嵌套循环,将所有字段都设置为vacio
。 此外,您不会在任何地方调用initBoard
。 我猜它打算在调用printTablero
之前在main
调用。
最后,发生分段错误是因为您试图取消引用在打印数组时未初始化的指针(因为未调用initBoard
因此未初始化任何内容),从而导致未定义的行为。
您没有对整个电路板的每个单元进行初始化。 您只需要使用ie vacio
初始化它们,然后将另一个设置为cell。
#include <stdio.h>
#include <stdlib.h>
char * tablero[9][9];
char Rey = 'R';
char gralOro = 'O';
char gralPlata = 'P';
char caballos = 'C';
char lanceros = 'L';
char alfil = 'A';
char torre = 'T';
char peones = 'p';
char vacio = ' ';
char *piezasNegrasPerdidas[20];
char *piezasBlancasPerdidas[20];
void initBoard(){
for(int i = 0; i<9; i++){
for(int j = 0; j<9; j++){
if(i==2 || i==6){
tablero[i][j] = &peones;
}
else {
tablero[i][j]=&vacio;
}
}
}
tablero[0][0] = &lanceros;
tablero[0][1] = &caballos;
tablero[0][2] = &gralPlata;
tablero[0][3] = &gralOro;
tablero[0][4] = &Rey;
tablero[0][5] = &gralOro;
tablero[0][6] = &gralPlata;
tablero[0][7] = &caballos;
tablero[0][8] = &lanceros;
tablero[1][1] = &alfil;
tablero[1][7] = &torre;
tablero[8][0] = &lanceros;
tablero[8][1] = &caballos;
tablero[8][2] = &gralPlata;
tablero[8][3] = &gralOro;
tablero[8][4] = &Rey;
tablero[8][5] = &gralOro;
tablero[8][6] = &gralPlata;
tablero[8][7] = &caballos;
tablero[8][8] = &lanceros;
tablero[7][1] = &alfil;
tablero[7][7] = &torre;
}
void printTablero(){
for (int r = 0; r < 9; r++){
for(int c = 0; c<9; c++){
printf("[%c]", *(tablero[r][c]));
}
printf("\n");
}
}
int main(){
initBoard();
printTablero();
return 0;
}
输出 :
[L][C][P][O][R][O][P][C][L]
[ ][A][ ][ ][ ][ ][ ][T][ ]
[p][p][p][p][p][p][p][p][p]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[p][p][p][p][p][p][p][p][p]
[ ][A][ ][ ][ ][ ][ ][T][ ]
[L][C][P][O][R][O][P][C][L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.