简体   繁体   English

是否可以在运行期间在结构中设置数组的大小?

[英]Is it possible to set the size of an array within structure during runtime?

I have a structure as below: 我的结构如下:

struct Query {  
    int     pages[];
    int     currentpage;
};

I was wondering if it was possible to set the size of this array after creating the structure. 我想知道在创建结构后是否可以设置此数组的大小。

Query new = malloc(sizeof(struct Query));

After this, I will perform some calculations which will then tell me the size that pages[] needs to be. 在此之后,我将执行一些计算,然后告诉我pages[]需要的大小。 If pages[] needed to be of size 4, how can I set it as such? 如果pages[]需要大小为4,我该如何设置它?

In C99 you can use Flexible array members : 在C99中,您可以使用Flexible数组成员

struct Query {  
    int currentpage;
    int pages[]; /* Must be the last member */
};

struct Query *new = malloc(sizeof(struct Query) + sizeof(int) * 4);

Change the type of pages member to pointer. pages成员的类型更改为指针。

struct Query {  
    int *pages;
    int currentpage;
};

struct Query *test = malloc(sizeof(struct Query));

if (test != NULL)
{
   //your calculations

   test->pages = malloc(result_of_your_calcs);
   if (test->pages != NULL)
   {
      // YOUR STUFF
   }
   else
   {
      // ERROR
   }
}
else
{
   // ERROR
}

When you'll free your struct, you have to do that on the contrary. 当你free你的结构时,你必须这样做。

free(test->pages);
free(test);

You can use a Flexible array member (details in @AlterMann's answer ) ( C99+ ), or a Zero length array ( GNU C ). 您可以使用Flexible数组成员@ AlterMann的答案中的详细信息)( C99 + )或零长度数组( GNU C )。

Quoting from https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html , 引自https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure that is really a header for a variable-length object: 在GNU C中允许零长度数组。它们作为结构的最后一个元素非常有用,它实际上是一个可变长度对象的头:

 struct line { int length; char contents[0]; }; struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length; 

For standard C90, the linked site mentions 对于标准C90,链接网站提及

In ISO C90, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc . 在ISO C90中,您必须提供长度为1的contents ,这意味着您要浪费空间或将参数复杂malloc

This means that for the code to work in standard C90/C89, char contents[0]; 这意味着代码在标准C90 / C89中工作, char contents[0]; should be char contents[1]; 应该是char contents[1]; .

The best solution would be to use pointers to int instead of array. 最好的解决方案是使用指向int而不是数组的指针。

You will need to change : 你需要改变:

int pages[];

to : 至 :

int *pages;

and then dynamically allocate it like this : 然后像这样动态分配它:

Query *new = malloc(sizeof(struct Query));
if (new == NULL)
    printf ("Error\n");
else
{
    new->pages = malloc(4*sizeof(int));
    if (new->pages == NULL)
       printf ("Error\n");
}

Otherwise if you want to keep your format, you will use C99 mode. 否则,如果要保留格式,则使用C99模式。 Declare pages as the last member of your struct, like this : pages声明为结构的最后一个成员,如下所示:

struct Query {  
    int currentpage;
    int pages[];
};

and then do : 然后做:

Query *new = malloc(sizeof(struct Query) + 4*sizeof(int));

Declare it as pointer and use malloc afterwards 将其声明为指针并在之后使用malloc

struct Query {  
    int * pages;
    int   currentpage;
};

. . .

struct Query obj;
obj.pages = malloc(n * sizeof(int));   // n is the length you want

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

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