指针的 scanf 函数不起作用(对于使用指针到指针的矩阵)
scanf function for pointer is not working (for a matrix using pointer to pointer)
这是我编写的用于将两个矩阵相乘的程序。
#include <stdio.h>
#include <stdlib.h>
void allocate(int **mat,int m,int n)
{
int i;
mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(mat+i) = (int*)malloc(n*sizeof(int));
}
void read(int **mat,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter the element in row number %d and column number %d\n",i+1,j+1);
scanf("%d",*(mat+i)+j);
}
}
void multiply(int **mat1,int m,int n,int **mat2,int p,int **prod)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
*(*(prod+i)+j) = 0;
for(k=0;k<n;k++)
*(*(prod+i)+j) += (*(*(mat1+i)+k))*(*(*(mat2+k)+j));
}
}
void PRINT(int **mat,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",*(*(mat+i)+j));
}
printf("\n\n\n");
}
}
int main()
{
int m,n,p,**mat1,**mat2,**prod;
printf("Enter the number of rows of the first matrix to be multiplied\n");
scanf("%d",&m);
printf("Enter the number of columns of the first matrix to be multiplied\n");
scanf("%d",&n);
printf("Enter the number of columns of the second matrix to be multiplied\n");
scanf("%d",&p);
allocate(mat1,m,n);
allocate(mat2,n,p);
allocate(prod,m,p);
printf("Enter the entries of the first matrix\n");
read(mat1,m,n);
printf("Enter the entries of the second matrix\n");
read(mat2,n,p);
printf("The first input matrix is\n");
PRINT(mat1,m,n);
printf("The second input matrix is\n");
PRINT(mat2,n,p);
multiply(mat1,m,n,mat2,p,prod);
printf("The product matrix is\n");
PRINT(prod,m,p);
return 0;
}
read
函数定义中使用的 scanf
函数不工作,它不允许我们提供任何输入并意外停止。我在另一个程序中以相同的方式使用它来查找矩阵的踪迹并且在那里很好。
请帮我找出错误。
你的分配函数非常错误。
当前,您在 allocate
中分配了局部变量 mat
并且您没有 return 它也没有通过指针传递它,因此,该值在结束时丢失函数(在 c 语言中,参数总是按值传递)并且您写入无效数据。你有一个未定义的行为,幸运的是,你的程序出现了段错误。
因此,当你分配一个数据时,你必须分配一个指向这个数据的指针。这里你的数据是 int**
。这意味着您的分配函数必须将 int***
作为输入。
您的程序应该适用于:
void allocate(int ***mat, int m,int n)
{
int i;
*mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(*mat+i) = (int*)malloc(n*sizeof(int));
}
处理该问题的另一种(可能更简洁)方法是 return 分配的垫子,如下所示
int** allocate(int m,int n)
{
int i;
int **mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(mat+i) = (int*)malloc(n*sizeof(int));
return mat;
}
此外,要调试此类问题,下次 valgrind 可能会很有帮助。
您的 allocate
函数接收作为其第一个参数传递的 int **
的 copy,因此,在您的 main
函数不被它修改。
为了解决这个问题,您 可以 将参数作为 "pointer to int**" 传递(即 int*** mat
)——但这开始变得混乱(并且您需要相应地调整 allocate
中的代码)。更好的方法是将 allocate
函数重新定义为 return 创建的指针,这样:
int** allocate(int m, int n)
{
int i;
int** mat = malloc(m * sizeof(int*));
for (i = 0; i < m; i++)
*(mat + i) = malloc(n * sizeof(int));
return mat;
}
然后,在您的 main
函数中,修改对 allocate
的调用,如下所示:
mat1 = allocate(m, n);
mat2 = allocate(n, p);
prod = allocate(m, p);
您(当然)需要适当的代码(在某些时候)来释放分配的矩阵。
另请参阅 Do I cast the result of malloc?
这是我编写的用于将两个矩阵相乘的程序。
#include <stdio.h>
#include <stdlib.h>
void allocate(int **mat,int m,int n)
{
int i;
mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(mat+i) = (int*)malloc(n*sizeof(int));
}
void read(int **mat,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter the element in row number %d and column number %d\n",i+1,j+1);
scanf("%d",*(mat+i)+j);
}
}
void multiply(int **mat1,int m,int n,int **mat2,int p,int **prod)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
*(*(prod+i)+j) = 0;
for(k=0;k<n;k++)
*(*(prod+i)+j) += (*(*(mat1+i)+k))*(*(*(mat2+k)+j));
}
}
void PRINT(int **mat,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",*(*(mat+i)+j));
}
printf("\n\n\n");
}
}
int main()
{
int m,n,p,**mat1,**mat2,**prod;
printf("Enter the number of rows of the first matrix to be multiplied\n");
scanf("%d",&m);
printf("Enter the number of columns of the first matrix to be multiplied\n");
scanf("%d",&n);
printf("Enter the number of columns of the second matrix to be multiplied\n");
scanf("%d",&p);
allocate(mat1,m,n);
allocate(mat2,n,p);
allocate(prod,m,p);
printf("Enter the entries of the first matrix\n");
read(mat1,m,n);
printf("Enter the entries of the second matrix\n");
read(mat2,n,p);
printf("The first input matrix is\n");
PRINT(mat1,m,n);
printf("The second input matrix is\n");
PRINT(mat2,n,p);
multiply(mat1,m,n,mat2,p,prod);
printf("The product matrix is\n");
PRINT(prod,m,p);
return 0;
}
read
函数定义中使用的 scanf
函数不工作,它不允许我们提供任何输入并意外停止。我在另一个程序中以相同的方式使用它来查找矩阵的踪迹并且在那里很好。
请帮我找出错误。
你的分配函数非常错误。
当前,您在 allocate
中分配了局部变量 mat
并且您没有 return 它也没有通过指针传递它,因此,该值在结束时丢失函数(在 c 语言中,参数总是按值传递)并且您写入无效数据。你有一个未定义的行为,幸运的是,你的程序出现了段错误。
因此,当你分配一个数据时,你必须分配一个指向这个数据的指针。这里你的数据是 int**
。这意味着您的分配函数必须将 int***
作为输入。
您的程序应该适用于:
void allocate(int ***mat, int m,int n)
{
int i;
*mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(*mat+i) = (int*)malloc(n*sizeof(int));
}
处理该问题的另一种(可能更简洁)方法是 return 分配的垫子,如下所示
int** allocate(int m,int n)
{
int i;
int **mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(mat+i) = (int*)malloc(n*sizeof(int));
return mat;
}
此外,要调试此类问题,下次 valgrind 可能会很有帮助。
您的 allocate
函数接收作为其第一个参数传递的 int **
的 copy,因此,在您的 main
函数不被它修改。
为了解决这个问题,您 可以 将参数作为 "pointer to int**" 传递(即 int*** mat
)——但这开始变得混乱(并且您需要相应地调整 allocate
中的代码)。更好的方法是将 allocate
函数重新定义为 return 创建的指针,这样:
int** allocate(int m, int n)
{
int i;
int** mat = malloc(m * sizeof(int*));
for (i = 0; i < m; i++)
*(mat + i) = malloc(n * sizeof(int));
return mat;
}
然后,在您的 main
函数中,修改对 allocate
的调用,如下所示:
mat1 = allocate(m, n);
mat2 = allocate(n, p);
prod = allocate(m, p);
您(当然)需要适当的代码(在某些时候)来释放分配的矩阵。
另请参阅 Do I cast the result of malloc?