Error: segmentation code dump fault in c programming
Error: segmentation code dump fault in c programming
下面的代码显示了代码转储段错误,谁能帮我解决一下,让代码运行顺利
#include<stdio.h>
int n,m,p;
int** send(int (*a)[m],int (*b)[p] )
{
int **c;
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
{c[i][j]=0;
for(int k=0;k<m;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
return c;
}
int main()
{
printf("Enter the number of rows for the first matrix:");
scanf("%d",&n);
printf("Enter the number of columns for the first matrix/the number of rows for the second matrix:");
scanf("%d",&m);
printf("Enter the number of columns for the second matrix:");
scanf("%d",&p);
int a[n][m],b[m][p];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("Enter value for a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<p;j++)
{
printf("Enter value for b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
int** (ptr)(int()[m],int(*)[p])=send;
int**c=ptr(a,b);
printf("The multiplication matrix is:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
以下 c 编程代码显示代码转储 - 分段错误,有人可以帮我解决以下错误以使代码 运行 顺利
我不会使用 VLA,所以我更改了它。我没有,但你不要忘记释放。正如@TomKarzes 所说,您忘记在发送函数中为 c 分配位置。
#include<stdio.h>
#include <malloc.h>
int n,m,p;
int** send(int **a,int **b ) //used pointers as inputs
{
int **c;
c = malloc(sizeof (int *) * n);
for (int i = 0; i < n; ++i) {
c[i] = malloc(sizeof (int ) * p);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
{
c[i][j]=0;
for(int k=0;k<m;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
return c;
}
int main()
{
printf("Enter the number of rows for the first matrix:");
scanf("%d",&n);
printf("Enter the number of columns for the first matrix/the number of rows for the second matrix:");
scanf("%d",&m);
printf("Enter the number of columns for the second matrix:");
scanf("%d",&p);
int **a, **b; //changed them to pointers as well
//allocation
a = malloc(sizeof (int*) * n);
for(int i = 0 ; i < n ; i++){
a[i] = malloc(sizeof (int) * m);
}
b = malloc(sizeof (int *) * m);
for (int i = 0; i < m; ++i) {
b[i] = malloc(sizeof (int) * p);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("Enter value for a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<p;j++)
{
printf("Enter value for b[%d][%d]:",i,j);
scanf("%d", &b[i][j]);
}
}
//int** (ptr)(int()[m],int(*)[p])=send;
int**c=send(a,b);
printf("The multiplication matrix is:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
它适用于 3x3 3x3 矩阵乘法。
下面的代码显示了代码转储段错误,谁能帮我解决一下,让代码运行顺利
#include<stdio.h>
int n,m,p;
int** send(int (*a)[m],int (*b)[p] )
{
int **c;
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
{c[i][j]=0;
for(int k=0;k<m;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
return c;
}
int main()
{
printf("Enter the number of rows for the first matrix:");
scanf("%d",&n);
printf("Enter the number of columns for the first matrix/the number of rows for the second matrix:");
scanf("%d",&m);
printf("Enter the number of columns for the second matrix:");
scanf("%d",&p);
int a[n][m],b[m][p];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("Enter value for a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<p;j++)
{
printf("Enter value for b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
int** (ptr)(int()[m],int(*)[p])=send;
int**c=ptr(a,b);
printf("The multiplication matrix is:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
以下 c 编程代码显示代码转储 - 分段错误,有人可以帮我解决以下错误以使代码 运行 顺利
我不会使用 VLA,所以我更改了它。我没有,但你不要忘记释放。正如@TomKarzes 所说,您忘记在发送函数中为 c 分配位置。
#include<stdio.h>
#include <malloc.h>
int n,m,p;
int** send(int **a,int **b ) //used pointers as inputs
{
int **c;
c = malloc(sizeof (int *) * n);
for (int i = 0; i < n; ++i) {
c[i] = malloc(sizeof (int ) * p);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
{
c[i][j]=0;
for(int k=0;k<m;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
return c;
}
int main()
{
printf("Enter the number of rows for the first matrix:");
scanf("%d",&n);
printf("Enter the number of columns for the first matrix/the number of rows for the second matrix:");
scanf("%d",&m);
printf("Enter the number of columns for the second matrix:");
scanf("%d",&p);
int **a, **b; //changed them to pointers as well
//allocation
a = malloc(sizeof (int*) * n);
for(int i = 0 ; i < n ; i++){
a[i] = malloc(sizeof (int) * m);
}
b = malloc(sizeof (int *) * m);
for (int i = 0; i < m; ++i) {
b[i] = malloc(sizeof (int) * p);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("Enter value for a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<p;j++)
{
printf("Enter value for b[%d][%d]:",i,j);
scanf("%d", &b[i][j]);
}
}
//int** (ptr)(int()[m],int(*)[p])=send;
int**c=send(a,b);
printf("The multiplication matrix is:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<p;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}
它适用于 3x3 3x3 矩阵乘法。