Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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_Matrix - Fatal编程技术网

C 编写一个程序,使用函数和双指针查找给定的矩阵是否为空?

C 编写一个程序,使用函数和双指针查找给定的矩阵是否为空?,c,matrix,C,Matrix,在下面的程序中,函数调用后执行停止,请告诉我为什么以及如何解决此问题 #include<stdio.h> int checkNull(int **a,int m,int n) { int null=0,i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) if(a[i][j]==0) null++; return null; } int main() { int a[10][10],null,i

在下面的程序中,函数调用后执行停止,请告诉我为什么以及如何解决此问题

#include<stdio.h>
int checkNull(int **a,int m,int n)
{
  int null=0,i,j;
  for(i=0;i<m;i++)
    for(j=0;j<n;j++)
      if(a[i][j]==0)
         null++;
  return null;
}
int main()
{
  int a[10][10],null,i,j,m,n;
  printf("Enter the number of rows in the matrix");
  scanf("%d",&m);
  printf("\nEnter the number of columns in the matrix");
  scanf("%d",&n);
  printf("\nEnter the elements in the matrix");
  for(i=0;i<m;i++)
    for(j=0;j<n;j++)
      scanf("%d",&a[i][j]);
    printf("\nThe matrix is");
  for(i=0;i<m;i++)
  {
    printf("\n");
    for(j=0;j<n;j++)
      printf("%d ",a[i][j]);
  }
    null=checkNull((int **)a,m,n);
  if(null==m*n)
    printf("\nThe matrix is null");
  else
    printf("\nThe matrix is not null");  
  return 0;
}
#包括
int checkNull(int**a、int m、int n)
{
int null=0,i,j;

对于(i=0;i此函数调用是错误的:

null=checkNull((int **)a,m,n);
a
显式转换为
int**
是不对的

根据
a
的定义,需要将函数
checkNull
更改为:

int checkNull(int (*a)[10], int m, int n) { ... }
int checkNull(int (*a)[20], int m, int n) { ... }
并使用

null=checkNull(a, m, n);
如果您使用:

int a[10][20];
然后,您需要将
checkNull
更改为:

int checkNull(int (*a)[10], int m, int n) { ... }
int checkNull(int (*a)[20], int m, int n) { ... }

最好让
checkNull()
函数实际检查null。我会将其更改为:

int checkNull(int a[][10],int m,int n)
{
  int i,j;
  for(i=0;i<m;i++)
    for(j=0;j<n;j++)
      if(a[i][j]!=0)
         return 0;
  return 1;
}
然而,从问题来看,您似乎希望使用双指针解决此问题。如果您创建了指向每行的指针数组,然后动态分配所有内容,则这是有意义的。这还有一个优点,即它适用于任何大小的矩阵。类似于以下内容:

#include <stdio.h>
#include <stdlib.h>
int checkNull(int **a, int m, int n)
{
  int *row, i, j;
  for(i=0; i<m; i++)
  {
    row = a[i];
    for(j=0; j<n; j++)
      if(row[j] != 0)
        return 0;
  }
  return 1;
}

int main()
{
  int **a, i, j, m, n;
  printf("Enter the number of rows in the matrix");
  scanf("%d", &m);
  a = (int **)malloc(sizeof(int *) * m);
  printf("\nEnter the number of columns in the matrix");
  scanf("%d", &n);
  printf("\nEnter the elements in the matrix");
  for(i = 0; i < m; i++)
  {
    a[i] = (int *)malloc(sizeof(int) * n);
    for(j = 0; j < n; j++)
      scanf("%d", a[i] + j);
  }
  printf("\nThe matrix is");
  for(i = 0; i < m; i++)
  {
    printf("\n");
    for(j = 0; j < n; j++)
      printf("%d ", a[i][j]);
  }
  if(checkNull(a, m, n))
    printf("\nThe matrix is null");
  else
    printf("\nThe matrix is not null");
  return 0;
}
#包括
#包括
int checkNull(int**a、int m、int n)
{
int*行,i,j;
对于(i=0;i