将二维数组 C 代码转换为 malloc

标签 c arrays matrix 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 A\n");
   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 B\n");
   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 A\n");
   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 B\n");
   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 A\n");
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 A\n");
// etc.

最后,free(first);。无需进行其他更改。

<小时/>

在尝试使用 malloc 时,您没有分配足够的空间,也没有 scanf 进入您分配的空间(而是覆盖了指向已分配空间的指针)。

关于将二维数组 C 代码转换为 malloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788345/

相关文章:

c - 将方法的值存储在数组中而不进行打印和数组比较问题

arrays - pg 删除数组中的公共(public)部分

c - C 中的多行 CSV 文件到矩阵

c++ - 错误 : expected primary-expression before 'float'

c - 在互斥锁定后递增共享变量不返回预期输出

c++ - .o 文件与 .a 文件

c - 为什么余数不适用于 Float 值?

java - 2D ArrayList Java 中元素的索引

r - 在 R 中将大矩阵转换为二进制评分矩阵?

c - 家庭作业问题,国际象棋皇后的可用字段