我试图做滑块拼图,但编译后有一个警告:
puzzle.c:在函数'main'中:puzzle.c:50:6:警告:传递'choice'的参数1使指针从整数不进行强制转换[默认启用]puzzle.c:2:5:注意:预期'int (*)[4]' 但参数是 'int' 类型</p>
当我运行程序时,当程序进入该功能时,它会给出 SEGMENTATION FAULT
我的程序在下面
int choice(int b[4][4],int *x,int y,int z,int c);
int main()
{
int i,j,row,col,p,s,c;
p=33;
int w[4][4]={{ 2, 3, 4, 5},\
{ 7, 9,11,12},\
{13,15,19,22},\
{34,45,65,-1}};
int a[4][4]={{15,12, 3, 2},\
{ 4, 7,13,65},\
{34, 9,45,22},\
{5,11,19,-1}};
printf("Welcome to the puzzle the puzzle matrix is below... enjoy!\n");
for(i=0;i<4;i++)
{
printf("\n");
for(j=0;j<4;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("-1 is the empty block\n");
printf("To exit Enter 0 and to continue Enter 1\n");
printf("Do you want to start or exit\n");
scanf("%d",&s);
while(s==1)
{
choicer:
{
printf("Enter the block you want to move\n");
printf("Enter the row number\n");
scanf("%d",&row);
printf("Enter the column number\n");
scanf("%d",&col);
}
if(row>3||col>3)
{
printf("Invalid row or col numbers\n");
goto choicer;
}
choicel:
{
printf("The choices to move the block are :\n 2= right,\n 3=left,\n 4=up,\n 5=down\n");
printf("Enter the choice\n");
scanf("%d",&c);
}
if(c!=2 && c!=3 && c!=4 && c!=5)
{
printf("Invalid choice");
goto choicel;
}
choice(a[4][4],&p,row,col,c);
for(i=0;i<4;i++)
{
printf("\n");
for(j=0;j<4;j++)
{
printf("%d",a[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]==w[i][j])
{
printf("Game finished.. YOU WON!!!");
}
}
}
printf("Do you want to exit? \n 0=exit ,\n 1=continue\n");
scanf("%d",&s);
}
}
int choice(int b[4][4],int *x,int y,int z,int c)
{
int temp;
if(c==4)
{
if(*x!=(((y-1)*10)+z))
{
printf("Invalid move");
return b[4][4];
}
else
{
temp = b[y][z];
b[y][z]=b[y-1][z];
b[y-1][z]=temp;
*x=(((y-1)*10)+z);
return b[4][4];
}
}
else if(c==5)
{
if(*x!=(((y+1)*10)+z))
{
printf("Invalid move");
return b[4][4];
}
else
{
temp = b[y][z];
b[y][z]=b[y+1][z];
b[y+1][z]=temp;
*x=(((y+1)*10)+z);
return b[4][4];
}
}
else if(c==2)
{
if(*x!=((y*10)+(z-1)))
{
printf("Invalid Move");
return b[4][4];
}
else
{
temp=b[y][z];
b[y][z]=b[y][z-1];
b[y][z-1]=temp;
*x=((y*10)+(z-1));
return b[4][4];
}
}
else
{
if(*x!=((y*10)+(z+1)))
{
printf("Invalid Move");
return b[4][4];
}
else
{
temp=b[y][z];
b[y][z]=b[y][z+1];
b[y][z+1]=temp;
*x=((y*10)+(z+1));
return b[4][4];
}
}
}