将2d数组C代码转换为malloc

我做了一个程序,增加了两个矩阵,并显示它们的总和,最大维数为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;
}

现在我需要改变它,所以没有每个矩阵的最大尺寸。 所以我需要使用malloc来创建我的数组。 所以我不能使用int A [rows] [cols]。 这是我将数组转换为malloc所做的。 它编译,但输入所有整数后崩溃。 需要帮忙。

/* 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;
}

首先,你不需要使用malloc。 只需在输入mn之后移动数组定义即可:

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]);

对于secondsum也是类似的。

如果矩阵可能大于100x100,那么使用malloc来避免堆栈溢出的风险可能会很好; 要做到这一点的方法是:

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

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

最后, free(first); 。 不需要其他更改。


在你尝试使用malloc的时候,你没有分配足够的空间,而且你也没有scanf到你分配的空间(而是覆盖了指向分配空间的指针)。

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

上一篇: Converting 2d array C code to malloc

下一篇: Finding the sum of elements of the largest sub array of increasing order in C?