Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何将矩阵存储在C中的数组中?_Arrays_C_Matrix_Multidimensional Array - Fatal编程技术网

Arrays 如何将矩阵存储在C中的数组中?

Arrays 如何将矩阵存储在C中的数组中?,arrays,c,matrix,multidimensional-array,Arrays,C,Matrix,Multidimensional Array,所以我想在一个数组中存储两个矩阵。我知道你可以做一个三维阵列。我想做的是能够将从AndMatrix方法获得的矩阵存储在一个数组中,然后在需要时使用它们。我的代码如下。arrayOfMatrices变量是我已经初始化的三维数组。有人能解释一下我如何访问数组中的这些矩阵吗。我的代码如下: int** AndMatrix(int **original,int **matA, int **matB, int row, int column){ int** result=calloc(row,

所以我想在一个数组中存储两个矩阵。我知道你可以做一个三维阵列。我想做的是能够将从AndMatrix方法获得的矩阵存储在一个数组中,然后在需要时使用它们。我的代码如下。arrayOfMatrices变量是我已经初始化的三维数组。有人能解释一下我如何访问数组中的这些矩阵吗。我的代码如下:

int** AndMatrix(int **original,int **matA, int **matB, int row, int column){
     int** result=calloc(row, sizeof(int*));
      for (int i = 0; i < row; i++) {
            result[i] = calloc(column, sizeof(int)); }

        return result;}
char temps[10][rows][columns];
arrayOfMatrices[countForMatrices] = AndMatrix(matrix,matrix1, matrix2, rows,columns);
int**and矩阵(int**original,int**matA,int**matB,int row,int column){
int**result=calloc(行,大小f(int*));
对于(int i=0;i
声明一个双指针数组:

int **arrayOfMatrices[total_matrices];

要访问存储在数组中的矩阵,可以采用与从
1D
数组中访问给定元素相同的方法,即:

arrayOfMatrices[0]
要访问存储在阵列零位上的矩阵,
arrayOfMatrices[1]
从位置1开始访问矩阵,依此类推

一个运行示例:

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

int** AndMatrix(int row, int column){
     int** result = calloc(row, sizeof(int*));
     for (int i = 0; i < row; i++) 
            result[i] = calloc(column, sizeof(int)); 
     return result;
}


int main() {
    
    int ***arrayOfMatrices = malloc(sizeof(int**) * 100);
    int row_matrix1 = 10;
    int col_matrix1 = 10;
    arrayOfMatrices[0] = AndMatrix(row_matrix1, col_matrix1);
    int **first_matrix = arrayOfMatrices[0];
    
    // Fill up the matrix with some values
    for(int i = 0; i < row_matrix1; i++)
     for(int j = 0; j < col_matrix1; j++)
         first_matrix[i][j] = i * j;
 
    for(int i = 0; i < row_matrix1; i++){
     for(int j = 0; j < col_matrix1; j++)
         printf("%d ", first_matrix[i][j]);
     printf("\n");
    }
    
    // free the memory accordingly.
    
    return 0;
}
  
#包括
#包括
整数**和矩阵(整数行,整数列){
int**result=calloc(行,大小f(int*));
对于(int i=0;i
#include <stdio.h>
#include <stdlib.h>

int** AndMatrix(int row, int column){
     int** result = calloc(row, sizeof(int*));
     for (int i = 0; i < row; i++) 
            result[i] = calloc(column, sizeof(int)); 
     return result;
}


int main() {
    
    int ***arrayOfMatrices = malloc(sizeof(int**) * 100);
    int row_matrix1 = 10;
    int col_matrix1 = 10;
    arrayOfMatrices[0] = AndMatrix(row_matrix1, col_matrix1);
    int **first_matrix = arrayOfMatrices[0];
    
    // Fill up the matrix with some values
    for(int i = 0; i < row_matrix1; i++)
     for(int j = 0; j < col_matrix1; j++)
         first_matrix[i][j] = i * j;
 
    for(int i = 0; i < row_matrix1; i++){
     for(int j = 0; j < col_matrix1; j++)
         printf("%d ", first_matrix[i][j]);
     printf("\n");
    }
    
    // free the memory accordingly.
    
    return 0;
}