c - slider 拼图中的段错误

标签 c function segmentation-fault

我试图做 slider 拼图,但编译后出现警告:

puzzle.c: In function ‘main’: puzzle.c:50:6: warning: passing argument 1 of ‘choice’ makes pointer from integer without a cast [enabled by default] puzzle.c:2:5: note: expected ‘int (*)[4]’ but argument is of type ‘int’

当我运行程序时,当程序进入函数时,它会给出 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];

    }

    }

}

最佳答案

这里有很多奇怪的地方,但传递a(指向二维数组的指针)而不是a[4][4]可能就足够了(一个int)当你调用你的函数时。更不用说 a[4][4] 超出了数组的范围(最后一个元素是 a[3][3]... )这样您就可以在 choice 函数中实际操作数组的内容。

另一个问题:一旦图 block 与解决方案匹配,您将打印“您赢了”消息:

 for(i=0;i<4;i++)
 {
   for(j=0;j<4;j++)
   {
     if(a[i][j]==w[i][j])
       {
     printf("Game finished.. YOU WON!!!");
       }
   }
 }

您需要对所有相同的图 block 进行“AND”运算:

 int winFlag = 1;
 for(i=0;i<4;i++)
 {
   for(j=0;j<4;j++)
   {
     winFlag = winFlag && (a[i][j]==w[i][j]);
   }
 }

 if(winFlag) 
 {
   ...
 }

另请注意,使用 goto 跳转代码的方式被认为是糟糕的代码结构 - 通常最好将步骤包装在 while 循环中。

可能还有更多问题...

关于c - slider 拼图中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19297671/

相关文章:

c++ - 垃圾收集的概念与非 OOP 语言有何关系

c - line_c |= 064;/* 错误地设置了位 2,4 和 5 */.. Misra C 指南。这是怎么发生的?

c - C 中的 unicode 字符串比较

php - 此功能正在检查什么?

javascript - 如何将 var myKey = "keyP"映射到 functionP.myKey

c - 是否有任何资源或书籍可以帮助您了解 load_elf_binary 函数?

mysql - Hibernate 按位运算函数未注册

c++ - 用户定义指针双端队列中的段错误

c - 初始化结构段错误

c# - Xamarin Android 启动时崩溃 "Got a SIGSEGV"