2017-05-13 60 views
0

使用一个小控制台应用程序,该应用程序可对类似维度的矩阵上的值执行数学运算。 CreateMatrix()函数返回int **,它给出了数组的维数,现在我正在尝试输入并且遇到错误。我以前从未使用malloc,所以我想我错误地使用了某些东西。我会尝试省略任何你不需要的代码来查找我的问题。在动态分配的矩阵中输入值 - C

int rowInput, colInput; 
    int** customA, customB; 

    int main(void) { 
     printf("\nEnter the number of Rows: "); 
     scanf(" %i", &rowInput); 
     printf("\nEnter the number of Columns: "); 
     scanf(" %i", &colInput); 
     customA = CreateMatrix(colInput, rowInput); 
     for (int row = 0; row <= rowInput; row++) { 
      for (int column = 0; column <= colInput; column++) { 
      printf("Enter input for value at MatrixA[%d][%d]\n", row, column); 
      scanf(" %i", &customA[row][column]); 
      } 
     } 
     PrintMatrix(rowInput, colInput, customA); 
     printf(" \n"); 
     } 
    } 

CreateMatrix(),并包括在我的Header.h

#include <stdio.h> 
#include <stdlib.h> 
#define Row 2 
#define Col 5 
#define Max 10 

/** 
*Dynamically allocate memory for custom matrix based on desired dimensions input 
*@return int** to newly allocated matrix 
**/ 
int** CreateMatrix(int colInput, int rowInput); 

/** 
*Checks input for matrix Row and columns exceeding maximum allowed 
* 
*/ 
int CheckMaximums(int *rowInput, int *colInput); 

而且CreateMatrix声明()在我的CLibrary.c我在CMake的链接已经被定义。包含CheckMaximums()仅供您参考,因为它在CreateMatrix中使用。尽管如此,我还没有解决这个问题。

#include <Header.h> 

int** CreateMatrix(int colInput, int rowInput) { 
    int** customMatrix; 
    CheckMaximums(&rowInput, &colInput); 
    printf(" \n"); 
    customMatrix = (int**)malloc(rowInput); 
    for (int i = 0; i < colInput; i++) 
    customMatrix = (int*)malloc(colInput); 
    return customMatrix; 
} 

int CheckMaximums(int *rowInput, int *colInput) { 
    if (*rowInput > Max || *colInput > Max) { 
    if (*rowInput > Max && *colInput > Max) { 
     *rowInput = Max; 
     *colInput = Max; 
     printf("\nYour Row and Column sizes both exceed the maximum allowed values\n" 
      "Row size has been set to max value (10)\n" 
      "Column size has been set to max value (10)"); 
    } 
    else if (*rowInput > Max) { 
     *rowInput = Max; 
     printf("\nYour Row size exceeds the maximum allowed value\n" 
      "Row size has been set to max value (10)\n"); 
    } 
    else { 
     *colInput = Max; 
     printf("\nYour Column size exceeds the maximum allowed value\n" 
      "Column size has been set to max value (10)\n"); 
    } 
    } 
} 

在此先感谢,我知道这是很多要看,试图减少到最低限度!

+0

不错!如果您将此作为答案发布,我会接受它。想知道customMatrix [i]究竟发生了什么?它现在在工作,但我不知道我做错了什么,或者它为什么这样工作。另外,为什么需要更改malloc中的参数(colInput * sizeof(int))?谢谢你的帮助。 –

回答

1

在CreateMatirx中malloc需要知道要分配的总字节数。乘以每个元素的大小所需的元素数量。

customMatrix = malloc(rowInput * sizeof (int*);//each element is a pointer to int 
for (int i = 0; i < rowInput; i++) 
    customMatrix[i] = malloc(colInput * sizeof int);//each element is an int 

第一malloc的用于rowInput指针分配足够的内存。每个指针可以像索引customMatrix[0]customMatrix[rowInput - 1]一样被访问。
for循环遍历每个指针并为整数分配足够的内存。

主,改变< =到<在for循环,否则你访问超出分配的内存

for (int row = 0; row < rowInput; row++) { 
    for (int column = 0; column < colInput; column++) { 

malloc和scanf函数的返回应作为托运这些功能可能会失败