【发布时间】:2020-03-03 21:17:37
【问题描述】:
基本上我有矩阵并想使用结构存储它们,但我需要使用动态内存分配来完成它
typedef struct {
int mymatrix[5][5]; // the matrix
int column; // will hold column number
int row; // this one is for row
} mystruct;
那么我怎样才能把它变成动态内存样式呢?
typedef struct {
int **mymatrix; // will have the matrix
int column; // will hold column number
int row; // this one is for row
} mystruct;
是这样的吗?如果是这样,我在哪里给出 mymatrix 的大小?我也想将它作为一个结构数组,但我想在创建新对象时扩展该数组。如果我的问题不清楚,任何想法都会有所帮助,谢谢。
【问题讨论】:
-
Yout
int **mymatrix将是指向整数数组的指针(行)数组。所以首先分配指向行的指针数组,然后分配每一行的整数。 -
可以使用
malloc和realloc分配和重新分配您的结构数组。 -
有了这些提示,您应该能够解决您的问题。我认为这是家庭作业,所以我们不应该给出完整的解决方案。
-
您可能希望将数组的维度存储在
mystruct的某个位置。这是row和column的用途,还是其他用途? -
@IanAbbott 他们只是要显示矩阵有多少行和列
标签: c struct malloc realloc calloc