简体   繁体   English

对结构中的结构进行mallocing数组(C)

[英]Mallocing array of structs within a struct (C)

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. 我有一个struct为一家公司,公司的数据,第二struct为企业,协会收藏。 The second struct will contain variable length arrays of the first struct , the company data struct . 第二个struct将包含第一个struct可变长度数组,即公司数据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 . 将有许多核心公司,这个数字将是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: Company类型修改为以下内容会更简单,也可能更好:

typedef struct
{
    char   code[32];
    double sharePrice;
    int    numShares;
    double totalVal;
    double totalDebts;
} Company;

That saves the loops allocating the code elements. 这节省了分配代码元素的循环。

You might consider that you make the Consortium struct a bit simpler. 您可能会认为您使Consortium结构更简单一些。 Since you have the counts for each type, core and assoc, you can have just a single array, the first part of which is for core and the second part of which is for assoc. 由于你有每种类型,核心和关联的计数,你可以只有一个数组,第一部分是核心,第二部分是关于。

So your struct would look something like the following source (which has not been compiled and is just jotted down rather than tested so caveat emptor): 所以你的结构看起来像下面的源代码(它没有被编译,只是记下来而不是经过测试,因此需要注意):

typedef struct {
    int numCore;    // number of core companies, first part of m_companies
    int numAssoc;   // number of assoc companies, second part of m_companies
    Company m_companies[1];
} Consortium;

Then you would create your actual data structure by something like: 然后,您将通过以下方式创建实际数据结构:

Consortium *makeConsortium (int numCore, int numAssoc) {
  Consortium *pConsortium = malloc (sizeof(Consortium) + sizeof(Company) * (numCore, numAssoc));
  if (pConsortium) {
      pConsortium->numCore = numCore;
      pConsortium->numAssoc = numAssoc;
  }
  return pConsortium;
}

After this you could fill it in by some functions which indicate success or not: 在此之后,您可以通过一些表明成功与否的函数填写它:

int addCompanyCore (Consortium *pConsortium, int index, Company *pCompany) {
  int iRetStatus = 0;
  if (pConsortium && index < pConsortium->numCore) {
    pConsortium->m_companies[index] = *pCompany;
    iRetStatus = 1;
  }
  return iRetStatus;
}
int addCompanyAssoc (Consortium *pConsortium, int index, Company *pCompany) {
  int iRetStatus = 0;
  if (pConsortium && index < pConsortium->numAssoc) {
    index += pConsortium->numCore;
    pConsortium->m_companies[index] = *pCompany;
    iRetStatus = 1;
  }
  return iRetStatus;
}

And then you would access them with another set of helper functions. 然后你将使用另一组辅助函数访问它们。

Company  *getCompanyCore (Consortium *pConsortium, int index) {
  Company *pCompany = 0;
  if (pConsortium && index < pConsortium->numCore) {
    pCompany = pConsortium->m_companies + index;
  }
  return pCompany;
}
Company * getCompanyAssoc (Consortium *pConsortium, int index) {
  Company *pCompany = 0;
  if (pConsortium && index < pConsortium->numAssoc) {
    index += pConsortium->numCore;
    pCompany = pConsortium->m_companies + index;
  }
  return pCompany;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM