初始化结构的矩阵(双指针)成员

时间:2018-11-21 09:06:17

标签: c pointers matrix memory-management struct

我在指针方面度过了艰难的时光,有人可以帮我一点忙吗?

我正在尝试在struct数组中初始化一个double(double)指针,但是我在某种程度上做错了。。示例:

struct MyStruct
{
    double **matrix;
};

double **CalculateMatrix(...)
{
    double **matrix = (double**)malloc(ROWS * sizeof(double*));
    for (int i = 0; i < ROWS; i++)
        matrix[i] = (double*)malloc(COLS * sizeof(double));
    // + assign some values
    return matrix;
}

void Initialize(struct MyStruct *structs, int size)
{
    for (int i = 0; i < size; i++)
        // Here structs[i].matrix holds the correct matrix values
        structs[i].matrix = CalculateMatrix(...);
}

int main()
{
    struct MyStruct *structs = (struct MyStruct*)malloc(SIZE * sizeof(struct MyStruct));
    Initialize(structs, SIZE);
    // but once the function returns, matrix values are completely different
}

对不起,如果重复了,我什么也找不到

2 个答案:

答案 0 :(得分:1)

您在这里不需要全局变量。因此,您可以在main方法中声明,定义和初始化struct数组的大小以及矩阵的大小。

此外,您方法的名称具有误导性,我将其更改为可以向作者传达每个函数目的的东西。或者,更准确地说,您的方法可以完成多个任务。最好将任务划分为可重用性。

以下是帮助您入门的最小示例:

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

struct MyStruct
{
    double **matrix;
};

double **allocate_matrix(int rows, int cols)
{
    double **matrix = malloc(rows * sizeof(double*));
    for (int i = 0; i < rows; i++)
        matrix[i] = malloc(cols * sizeof(double));
    return matrix;
}

void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
    for (int i = 0; i < size; i++)
        structs[i].matrix = allocate_matrix(rows, cols);
}

void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
    for (int i = 0; i < size; i++)
        for(int j = 0; j < rows; j++)
            for(int z = 0; z < cols; z++)
                structs[i].matrix[j][z] = -1.2;
}

void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
    for (int i = 0; i < size; i++)
        for(int j = 0; j < rows; j++)
            for(int z = 0; z < cols; z++)
                printf("%f\n", structs[i].matrix[j][z]);
}

void free_matrices(struct MyStruct *structs, int size, int rows) {
    for (int i = 0; i < size; i++) {
        for(int j = 0; j < rows; j++) {
            free(structs[i].matrix[j]);
        }
        free(structs[i].matrix);
    }
}


int main()
{
    int rows = 3, cols = 4, size = 2;
    struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
    structs[0].matrix = NULL;
    structs[1].matrix = NULL;
    if(structs[0].matrix == NULL)
        printf("null\n");
    allocate_matrices(structs, size, rows, cols);
    if(structs[0].matrix == NULL)
        printf("null\n");
    fill_matrices(structs, size, rows, cols);
    print_matrices(structs, size, rows, cols);
    free_matrices(structs, size, rows);
    free(structs);
}

输出:

null
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000

从我的2D dynamic array (C)中汲取灵感。

答案 1 :(得分:0)

扩展gsamaras'answer

“您在这里不需要全局变量” 是事实。不过,具有常量ROWSCOLS 也是有意义的。这取决于用例!

gsamaras的方法允许您为每个矩阵定义单独的大小。如果要为所有创建的矩阵强制使用相同的大小,则没有常量的参数时,不易出错。