繁体   English   中英

错误:c 编程中的分段代码转储错误

[英]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 矩阵乘法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM