Mallocing一个结构中的结构的数组(C) [英] Mallocing array of structs within a struct (C)
问题描述
我无法找到答案这个问题;我发现我的解决这个问题,网上类似的例子,但没有。
我有一个结构
为公司,企业,以及第二数据结构
为企业的集合,财团。第二个结构
将包含的第一个结构
,公司数据结构<变长数组/ code>。可变长度数组的元素的数量将取决于公司在财团的数目
我要动态分配无论是必需的,但我得到一个有点失落。这些都是结构:
typedef结构{
字符code [];
双sharePrice;
INT numShares;
双totalVal;
双totalDebts;
}公司;typedef结构{
INT numCore;
INT numAssoc;
公司的核心[];
公司ASSOC [];
}联盟;
将有一批核心企业,这个数字将在联盟结构
核心数组的大小。也是一样的联营公司。
我想出了这个前pression,但我不知道我错过了什么:
财团*驸马=((财团*)malloc的((numCore + numAssoc)*(sizeof的(联盟));
您需要使用指针,并分别分配数组:
typedef结构
{
字符* code;
双sharePrice;
INT numShares;
双totalVal;
双totalDebts;
}公司;typedef结构
{
INT numCore;
INT numAssoc;
公司*核心;
公司* assoc命令;
}联盟;联盟* C =的malloc(sizeof的(* C)); //错误检查
C-&GT; numCore = 4;
C-&GT;核心=的malloc(sizeof的(* C-&GT;芯)* C-&GT; numCore); //错误检查
C-&GT; numAssoc = 3;
C-&GT; ASSOC =的malloc(sizeof的(* C-&GT; assoc命令)* C-&GT; numAssoc); //错误检查的for(int i = 0; I&LT; C-&GT; numCore;我++)
C-方式&gt;的核心[I] code =的malloc(32); //错误检查的for(int i = 0; I&LT; C-&GT; numAssoc;我++)
C-方式&gt; assoc命令[I] code =的malloc(32); //错误检查//担心其他数据成员初始化!
这将是简单,可能更好修改公司
键入
typedef结构
{
字符code [32];
双sharePrice;
INT numShares;
双totalVal;
双totalDebts;
}公司;
这节省了循环分配code元素。
I'm having trouble finding the answer to this problem; I've found similar examples of this online but none that address my problem.
I have a struct
for the data for a company, Company, and a second struct
for collections of companies, Consortium. The second struct
will contain variable length arrays of the first struct
, the company data struct
. The number of elements of the variable length arrays will depend on the number of companies in a consortium.
I want to dynamically allocate whatever is required but I'm getting a bit lost. These are the structs:
typedef struct {
char code[];
double sharePrice;
int numShares;
double totalVal;
double totalDebts;
} Company;
typedef struct {
int numCore;
int numAssoc;
Company core[];
Company assoc[];
} Consortium;
There will be a number of core companies, and this number will be the size of the core array in the Consortium struct
. Same goes for associate companies.
I came up with this expression but I'm not sure what I'm missing:
Consortium *consort=((Consortium*)malloc((numCore+numAssoc)*(sizeof(Consortium));
You'll need to use pointers and allocate the arrays separately:
typedef struct
{
char *code;
double sharePrice;
int numShares;
double totalVal;
double totalDebts;
} Company;
typedef struct
{
int numCore;
int numAssoc;
Company *core;
Company *assoc;
} Consortium;
Consortium *c = malloc(sizeof(*c)); // Error check
c->numCore = 4;
c->core = malloc(sizeof(*c->core) * c->numCore); // Error check
c->numAssoc = 3;
c->assoc = malloc(sizeof(*c->assoc) * c->numAssoc); // Error check
for (int i = 0; i < c->numCore; i++)
c->core[i].code = malloc(32); // Error check
for (int i = 0; i < c->numAssoc; i++)
c->assoc[i].code = malloc(32); // Error check
// Worry about other data member initializations!
It would be simpler and possibly better to modify the Company
type to:
typedef struct
{
char code[32];
double sharePrice;
int numShares;
double totalVal;
double totalDebts;
} Company;
That saves the loops allocating the code elements.
这篇关于Mallocing一个结构中的结构的数组(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!