简体   繁体   中英

Error: segmentation code dump fault in c programming

the following code shows a code dump segmentation fault , can someone help me resolve it to make the code run smoothly

#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;
  
}

the following c programming code shows a code dump - segmentation fault , can someone help me resolve the following error to make the code run smoothly

I wouldn't use VLA so I changed it. I didn't but you don't forget to free. You forget to allocate place for c in send function as @TomKarzes said.

#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;

}

It worked for 3x3 3x3 matrix multiplication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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