Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在矩阵中搜索值的问题_C - Fatal编程技术网

C 在矩阵中搜索值的问题

C 在矩阵中搜索值的问题,c,C,我想做一个函数,通过给(矩阵+我想检查的值+行)作为参数来检查矩阵中是否存在值,我在从整数到指针的转换类型上得到了一个错误。请尽快为我提供帮助,谢谢:) #包括 #包括 #包括 布尔搜索(int*Mat[4][4],int val,int numlinge){ int i; 对于(i=0;i大量问题 int*Mat[4][4]它是指向int的指针数组。不是指向数组的指针。您需要int-Mat[4][4] Search(Mat[4][4],您不会只传递指向函数的指针,而只传递数组边界以外的整数值

我想做一个函数,通过给(矩阵+我想检查的值+行)作为参数来检查矩阵中是否存在值,我在从整数到指针的转换类型上得到了一个错误。请尽快为我提供帮助,谢谢:)

#包括
#包括
#包括
布尔搜索(int*Mat[4][4],int val,int numlinge){
int i;
对于(i=0;i大量问题

  • int*Mat[4][4]
    它是指向
    int
    的指针数组。不是指向数组的指针。您需要
    int-Mat[4][4]

  • Search(Mat[4][4],
    您不会只传递指向函数的指针,而只传递数组边界以外的整数值。一次调用2个UBs。您需要
    Search(Mat,
    此处


  • for(i=0;i您的代码中存在多个问题:

    • Search
      的原型应该是
      bool-Search(int-Mat[4][4],int-value,int-row);
      或者干脆是
      bool-Search(int-Mat[][4],int-value,int-row);
      。发布后,您声明
      Mat
      是指向
      int
      的指针矩阵
    • (i=0;i=0)的循环
      
      printf(“值%d位于位置Mat[%d][%d]\n”,5,1,col);
      其他的
      printf(“值%d不存在于Mat的第%d行,\n”,5,1);
      返回0;
      }
      
      您已经声明了
      Search
      这样第一个参数是指向4x4 2D矩阵的指针。但是,您的调用
      Search(Mat[4][4],5,1)
      作为第一个参数传递了对矩阵
      Mat
      中不存在的元素的无效引用。在位置4,4,
      Mat
      中没有元素。
      Mat[4][4]
      指的是一个整数,而函数需要一个指针,这就是为什么您会看到该特定错误。如果数组有4个元素,则有效索引为0到3。如果使用索引4,则访问数组超出范围。
      p=Search(Mat,5,1);
      是您希望收集2D数组作为函数参数的内容
      #include <stdlib.h>
      #include <stdio.h>
      #include <stdbool.h>
      
      bool Search(int *Mat[4][4], int val, int numLigne) {
          int i;
      
          for (i = 0; i <= 4; ++i) {
              if (Mat[numLigne][i] == val)
                  return 1;
              else
                  return 0;
          }
      }
      
      int main() {
          int Mat[4][4] = {
              { 1, 2, 3, 4 },
              { 5, 6, 7, 8 },
              { 9, 10, 11, 12 },
              { 13, 14, 15, 16 }
          };
          int p;
          p = Search(Mat[4][4], 5, 1);
          printf("The number is: %d", p);
      
          return 0;
      }
      
          for( i=0; i<=4; ++i )
          {
              if(Mat[numLigne][i] == val  )
                  return 1;
              else
                  return 0;
          }
      
              if(Mat[numLigne][0] == val  )
                  return 1;
              else
                  return 0;
      
      #include <stdio.h>
      
      int Search(int Mat[4][4], int val, int row) {
          for (int i = 0; i < 4; ++i) {
              if (Mat[row][i] == val)
                  return i;
          }
          return -1;  // not found
      }
      
      int main() {
          int Mat[4][4] = {
              { 1, 2, 3, 4 },
              { 5, 6, 7, 8 },
              { 9, 10, 11, 12 },
              { 13, 14, 15, 16 }
          };
          int col = Search(Mat, 5, 1);
          if (col >= 0)
              printf("The value %d is at position Mat[%d][%d]\n", 5, 1, col);
          else
              printf("The value %d is not present in row %d of Mat\n", 5, 1);
      
          return 0;
      }