C 当大小未知时创建指针数组(稀疏矩阵)

C 当大小未知时创建指针数组(稀疏矩阵),c,sparse-matrix,C,Sparse Matrix,考虑下面的代码,我试图创建一个包含指针数组的稀疏矩阵。在“struct Sparse_matrix”中,当大小未知时,这是创建指针数组的正确方法吗?C程序员解决这个问题的方法是什么?我是C的新手 谢谢你的帮助 #include "sparse_matrix.h" struct Node{ short data; unsigned short row; Node* next; }; struct SPARSE_MATRIX { unsigned short col

考虑下面的代码,我试图创建一个包含指针数组的稀疏矩阵。在“struct Sparse_matrix”中,当大小未知时,这是创建指针数组的正确方法吗?C程序员解决这个问题的方法是什么?我是C的新手

谢谢你的帮助

#include "sparse_matrix.h"
struct Node{
    short data;
    unsigned short row;
    Node* next;
};

struct SPARSE_MATRIX {
    unsigned short cols;
    Node* list[0];
};

SparseMatrix *create(unsigned short rows, unsigned short cols){
    SparseMatrix* matrix=malloc(cols*sizeof(Node*)+sizeof(unsigned short));
    matrix->cols=cols;

    for(int i=0;i<cols;i++){
        matrix->list[i]=NULL;
    }
    return matrix;
}
#包括“稀疏矩阵.h”
结构节点{
短数据;
无符号短行;
节点*下一步;
};
结构稀疏矩阵{
无符号短列;
节点*列表[0];
};
SparseMatrix*创建(无符号短行、无符号短列){
SparseMatrix*matrix=malloc(cols*sizeof(Node*)+sizeof(unsigned short));
矩阵->cols=cols;
for(int i=0;ilist[i]=NULL;
}
收益矩阵;
}
写得更好

  SparseMatrix* matrix=malloc(cols*sizeof(Node*)+sizeof(SparseMatrix));
因为路线和间隙