我想初始化一个包含不同类型变量的结构。例如,假设我有
struct population {
int *ids;
double *incomes;
struct good **goodsdistn; // This is the one I am having trouble with.
};
struct population popn;
我想popn
使用在另一个结构中定义的参数进行初始化,比如说
struct params {
int numpeople;
// there are other parameters here, not relevant for the question.
};
struct params parameters = {.numpeople = 50};
要初始化popn
,我正在考虑执行以下操作:
(1) 在外面定义如下函数 main()
void create_population(struct population *popn, struct params *parameters)
{
popn -> ids = malloc(sizeof(int) * parameters -> numpeople); //This works
popn -> incomes = malloc(sizeof(double) * parameters -> numpeople); //This works
popn -> goodsdistn = malloc(sizeof(???) * parameters -> numpeople);
// What do I put in place of ??? when I have a pointer to a pointer to struct good.
}
(2)在main()
调用这个函数初始化popn(稍后我可以填充结构成员):
create_population(&popn, ¶meters);
谢谢你的帮助。