我的结构如下:
struct Query {
int pages[];
int currentpage;
};
我想知道创建结构后是否可以设置此数组的大小。
Query new = malloc(sizeof(struct Query));
之后,我将执行一些计算,然后将告诉我所需的大小pages[]
。如果pages[]
需要大小为4,该如何设置呢?
将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
}
当您使用free
自己的结构时,您必须相反地进行操作。
free(test->pages);
free(test);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句