c语言 - 收到"warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast"错误



下面是我的MRE。从本质上讲,我正在制作一款棋盘游戏,我需要做两件事。制作电路板,每转一圈后打印。由于电路板在每转后都会发生变化,我在下面的两个单独的函数makeBoardprintBoard中有它们,所以我只是把它们放在MRE中。棋盘的尺寸也来自args,因此它可以是玩家选择的任何尺寸。但当我打印出空的板子时,上面写着上面提到的错误。

对于上下文,矩阵应该是5乘5,然后它应该像这样打印:

。。。….

edit:我无法测试矩阵是否以正确的格式打印,因为我收到了这个错误,所以如果代码的这部分有问题,我稍后会处理。

int main()
{
int x = 5;
int y = 5;

char board[5][5];

// this is where I am filling the board with its 'null' values
for(int i =0; i<x; i++){
for(int j =0; j<y; j++){
// this is where the error takes place
board[i][j]= ".";
}
}
// this is where I am printing the board, I have a specific format so, 
// that there is a space between each dot
for(int i=0; i<x; i++){
for(int j = 0; j<y; j++){
if(j<y-1){
printf("%c",board[i][j]);
printf(" ");
}
else if(j=y-1){
printf("%c", board[i][j]);
}
printf("n");

}
}
}

代替行

board[i][j]=".";

你应该写:

board[i][j]='.';

双引号("(用于字符串文字,单引号('(用于字符。

您收到此警告是因为字符串存储为指向第一个字符(char*(的指针。字符串内部的字符存储在从用于存储字符串的指针的值寻址到字符串中第一个空字符''(在存储器中(的存储器中。

此外,警告不是错误。错误会阻止代码的编译,而警告则会通知代码的潜在问题。

注意:索引使用unsigned int而不是int,因为默认情况下intsigned

相关内容

最新更新