c-通过循环打印数组元素时指向旧地址的指针



我想通过另一个用户定义的函数读取主函数中定义的数组元素。数组是2D的,它正确地显示了前三个元素,但当下一个循环开始时,指针指向的地址距离预期地址后退了2步。为什么?以下是调用frame((函数的主要函数,问题是:

void main(){
char dec,player[2][20];
int i,counter=0,palo,winner=0;
for(i=0;i<2;i++){
printf("Enter Player%d's name: ",(i+1));
scanf("%s",player[i]);                  //ASK PLAYER NAME
}
startAgain:                             //GAME RESTART POINT
system("cls");
palo=0;
char spot[][3]={"123","456","789"};
//------------------MAIN GAME AREA-------------------------------
for(counter=0;counter<9;counter++,palo++){
frame(*spot);
read(&palo,*spot,*player);
palo %=2;
}
}

这是frame((函数:

void frame(char *count){
int i,j;
printf("ttt");
line(24);
for (i = 0; i < 3; i++){
printf("ttt");
for (j = 0; j < 3; j++){
printf("|   %c   ",(*(count+i)+j));
}
printf("|nttt");
line(24);
}
}

预期输出为:

1        2       3
4        5       6
7        8       9

显示内容:

1        2       3
2        3       4
3        4       5

让自己和他人的生活更轻松,使用普通的数组索引而不是指针算术。

for(counter=0;counter<9;counter++,palo++){
frame(spot);
read(&palo,spot,player);
palo %=2;
}
...
void frame(char count[][3]){
int i,j;
printf("ttt");
line(24);
for (i = 0; i < 3; i++){
printf("ttt");
for (j = 0; j < 3; j++){
printf("|   %c   ",count[i][j]);
}
printf("|nttt");
line(24);
}
}

最新更新