Converting 2d array C code to malloc

I made a program that adds two matrices and displays their sum with a max dimension of 100.

/* This program asks the user for 2 matrices called A and B, as integers,
   and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>

// Construct function
void construct()
{
   int m, n, i, j;      // Variables
   int first[100][100], second[100][100], sum[100][100]; // Matrices variables

   printf("Please enter the number of rows: ");
   scanf("%d", &m);
   printf("Please enter the number of columns: ");
   scanf("%d", &n);

   // User enters m x n amount whole numbers for the Matrix A
   printf("Enter Matrix An");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first[i][j]);

   // User enters m x n amount whole numbers for the Matrix B
   printf("Enter Matrix Bn");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
            scanf("%d", &second[i][j]);

   // Adds the sum of Matrix A and Matrix B
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         sum[i][j] = first[i][j] + second[i][j];

   // Display the sum of Matrix A and Matrix B
   printf("A + B =n");
   for (i = 0; i < m; i++)
   {
      for (j = 0; j < n; j++)
         printf("%d ", sum[i][j]);

      printf("n"); // Prints new line
   }

   return ;
}

// Main Function
int main()
{
    construct(); // Calls construct function
    return 0;
}

Now I need to change it so there is no max size for each matrix. So I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers. Need help.

/* This program asks the user for 2 matrices called A and B, as integers,
   and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>
#include <stdlib.h>

// Construct function
void construct()
{
   int m, n, i, j;      // Variables
   int *first = NULL;
   int *second = NULL;
   int *sum = NULL;    // Matrices variables

   printf("Please enter the number of rows: ");
   scanf("%d", &m);
   printf("Please enter the number of columns: ");
   scanf("%d", &n);

   first = (int*)malloc(m * sizeof(int));
   second = (int*)malloc(n * sizeof(int));
   sum = (int*)malloc(m * n * sizeof(int));


   // User enters m x n amount whole numbers for the Matrix A
   printf("Enter Matrix An");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first);

   // User enters m x n amount whole numbers for the Matrix B
   printf("Enter Matrix Bn");
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
            scanf("%d", &second);

   // Adds the sum of Matrix A and Matrix B
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         sum = *first + *second;

   // Display the sum of Matrix A and Matrix B
   printf("A + B =n");
   for (i = 0; i < m; i++)
   {
      for (j = 0; j < n; j++)
         printf("%d ", sum);

      printf("n");
   }

   return ;
}

// Main Function
int main()
{
    construct(); // Calls construct function
    return 0;
}

First of all, you don't need to use malloc. Just move the array definitions to be after you have inputted m and n :

int first[m][n];

printf("Enter Matrix An");
for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
        scanf("%d", &first[i][j]);

and similarly for second and sum .

If the matrices might be larger than 100x100 then it might be good to use malloc to avoid the risk of a stack overflow; the way to do that is:

int (*first)[n] = malloc(m * sizeof *first);

printf("Enter Matrix An");
// etc.

and at the end, free(first); . No other changes are required.


In your attempt to use malloc, you didn't allocate enough space, and you didn't scanf into the space you allocated either (instead you overwrote the pointer to the allocated space).

链接地址: http://www.djcxy.com/p/85944.html

上一篇: 如何找到细胞索引号。 在2

下一篇: 将2d数组C代码转换为malloc