从输入C动态分配矩阵

我一直在尝试这段代码而且效果不佳。

void *matrix_allocate_variable (int size) { void *p1; if ((p1=(int*)malloc(size))==NULL){ printf("out of memory.\n"); exit(1); } return p1; } 

在这里,我创建了一个调用malloc并在出错时退出的函数,以便我可以在我的下一个函数中使用它:

 void matrix_new(int **matrices, int *row_counts, int *column_counts, char specifier) { int index, i; index= (int)(specifier-'A'); scanf("%d",&row_counts[index]); scanf("%d",&column_counts[index]); matrices[index]= (int*)matrix__allocate_variable(sizeof(int)* (row_counts[index]*column_counts[index]); 

这是我遇到问题的地方。 我正在尝试让用户输入一些用于创建矩阵的输入,但是我在尝试使其工作时遇到了很多问题。 有人可以帮我开始吗?

PS。 有关更多详细信息,我在functions.c中创建函数,这是我到目前为止所拥有的。 我有一个main.c调用这些函数,以便稍后我可以添加,减去和转置,但到目前为止,我正在尝试输入数据,这是我遇到很多麻烦。 这是我的main.c ,我称之为函数。

 /* Pointer to the set of matrix registers. */ int **matrices = NULL; /* row_counts[i] stores the number of rows in matrix i */ int *row_counts = NULL; /* column_counts[i] stores the number of columns in matrix i */ int *column_counts = NULL; /********************************************************************** Skeleton code part B: suggested form for selected variable initializations **********************************************************************/ /* Initialize the matrix set. */ matrices = (int**) matrix_allocate_variable(...); column_counts = (int *)matrix_allocate_variable(...); row_counts = (int *)matrix_allocate_variable(...); char call[2]; int error = 2; do { printf ( "> "); if (scanf ("%1s", call) !=1) { fprintf (stderr, "Command not found. \n"); exit (1); } switch (call [0]) { case 'A': matrix_new(matrices,row_counts,column_counts,'A'); break; case 'B': matrix_new(matrices,row_counts,column_counts,'B'); break; case 'C': matrix_new(matrices,row_counts,column_counts,'C'); break; case 'D': matrix_new(matrices,row_counts,column_counts,'D'); break; case '+': matrix_add(matrices,row_counts,column_counts); break; case '^': matrix_tranpose(matrices,row_counts,column_counts); break; case '*': matrix_multiply(matrices,row_counts,column_counts); break; case '$': exit (1); default : fprintf (stderr, "Command not found. \n"); } } while (error != 1); return 0; } 

任何帮助都会很好,我接下来应该做的任何指示都很棒。 非常感谢你们每一个人。

嗨,这是使用malloc创建一个矩阵的示例代码。
(这应该可以让你对如何创建矩阵数组有所了解。如果没有,请告诉我。)

 #include  #include  // Creates a matrix given the size of the matrix (rows * cols) int **CreateMatrix(int rows, int cols) { int **matrix = malloc(sizeof(int*) * rows); int row; for (row = 0; row < rows; row++) { matrix[row] = malloc(sizeof(int) * cols); } return matrix; } // Take input for the matrix. void MatrixInput(int **matrix, int rows, int cols) { int row, col; for (row = 0; row < rows; row++) { for (col = 0; col < cols; col++) { scanf("%d", &matrix[row][col]); } } } void PrintMatrix(int **matrix, int rows, int cols) { int row, col; for (row = 0; row< rows; row++) { for (col = 0; col < cols; col++) { printf("%d ", matrix[row][col]); } printf("\n"); } } int main() { int **matrix; int rows = 5; int cols = 4; matrix = CreateMatrix(rows, cols); MatrixInput(matrix, rows, cols); PrintMatrix(matrix, rows, cols); } 

这是一个为二维数组创建和释放内存的示例 ……
(可轻松修改其他类型)

 int ** Create2D(int **arr, int cols, int rows) { int space = cols*rows; int y; arr = calloc(space, sizeof(int)); for(y=0;y 

像这样用它:

 #include  int main(void) { int **array=0, i, j; array = Create2D(array, 5, 4);//get the actual row/column values from reading the file once. for(i=0;i<5;i++) for(j=0;j<4;j++) array[i][j]=i*j; //example values for illustration free2DInt(array, 5); return 0; }