颁布实施的结构动态数组 [英] Issue implementing dynamic array of structures
问题描述
我有创建结构的动态数组的问题。我已经看到,并试图实施这里的几个例子和其他网站,这些例子以及它们如何分配内存往往不同,我似乎无法得到任何他们为我工作。任何帮助将大大AP preciated。
typedef结构节点{
INT指数;
INT XMIN,XMAX,YMIN,YMAX;
} 划分;分区*第一部分=(分区*)malloc的(的sizeof(分区)* 50);
我甚至无法获得这项权利。它给了我以下错误:
错误:初始元素不是常数
如果任何人都可以解释如何这样的事情应该执行我将不胜AP preciate它。
此外,一旦我有一部分下来,如何将添加值到结构的要素是什么?会像下面的工作?
第一部分[I] - >指数= X;
编译器是抱怨,因为你正在做的:
分区*第1部分=(分区*)malloc的(的sizeof(分区)* 50);
做到这一点,而不是:
分区*第1部分;INT
主要(无效)
{ 第一部分=(分区*)malloc的(的sizeof(分区)* 50); ...
}
您使用一个版本的初始化的在全球,它在c必须是一个恒定值。由malloc的移动到一个功能,你是初始化值与code,但你没有使用的初始化的作为语言定义的。
同样,你可以有一个全球性的的已的初始化:
INT twenty_two = 22;
下面 22
是一个常数,因此允许的。
更新:这是一个有些冗长的例子,将显示最可能的方式:
的#define PARTMAX 50
分区static_partlist [PARTMAX]
分区* dynamic_partlist;INT grown_partmax;
分区* grown_partlist;空虚
iterate_byindex_static_length(分区*明细表)
{
INT IDX; 为(IDX = 0; idx的&下; PARTMAX ++ IDX)
do_something(安培;明细表[idx的]);
}空虚
iterate_byptr_static_length(分区*明细表)
{
分区* CUR;
分区*结束; //这些都是等价的:
//结束=明细表+ PARTMAX;
//结束=安培;明细表[PARTMAX]
结束=明细表+ PARTMAX; 对于(CUR =明细表; CUR< END; ++ CUR)
do_something(现);
}空虚
iterate_byindex_dynamic_length(分区*明细表,INT partmax)
{
INT IDX; 为(IDX = 0; idx的&下; partmax ++ IDX)
do_something(安培;明细表[idx的]);
}空虚
iterate_byptr_dynamic_length(分区*明细表,INT partmax)
{
分区* CUR;
分区*结束; //这些都是等价的:
//结束=明细表+ partmax;
//结束=安培;明细表[partmax]
结束=明细表+ partmax; 对于(CUR =明细表; CUR< END; ++ CUR)
do_something(现);
}INT
主要(无效)
{
分区*一部分; dynamic_partlist =的malloc(sizeof的(分区)* PARTMAX); //这些都是相同的
iterate_byindex_static_length(dynamic_partlist);
iterate_byindex_static_length(dynamic_partlist + 0);
iterate_byindex_static_length(安培; dynamic_partlist [0]); //因为这些
iterate_byptr_static_length(static_partlist);
iterate_byptr_static_length(static_partlist + 0);
iterate_byptr_static_length(安培; static_partlist [0]); // 还是一样 ...
iterate_byindex_dynamic_length(dynamic_partlist,PARTMAX);
iterate_byindex_dynamic_length(dynamic_partlist + 0 PARTMAX);
iterate_byindex_dynamic_length(安培; dynamic_partlist [0],PARTMAX); //再次同...
iterate_byptr_dynamic_length(static_partlist,PARTMAX);
iterate_byptr_dynamic_length(static_partlist + 0 PARTMAX);
iterate_byptr_dynamic_length(安培; static_partlist [0],PARTMAX); //让我们动态增长的数组和填充它...
为(IDX = 0; idx的小于10 ++ IDX){
//增长列表 - 请注意,realloc的是足够聪明的处理
//事实上,grown_partlist为NULL在第一时间通过
++ grown_partmax;
grown_partlist =的realloc(grown_partlist,
grown_partmax *的sizeof(分区)); 部分=安培; grown_partlist [grown_partmax - 1]; //填写与任何数据部分...
} //再一次,还是一样
iterate_byindex_dynamic_length(grown_partlist,grown_partmax);
iterate_byindex_dynamic_length(grown_partlist + 0 grown_partmax);
iterate_byindex_dynamic_length(安培; grown_partlist [0],grown_partmax); //啧,做事永远改变吗? :-)
iterate_byptr_dynamic_length(grown_partlist,grown_partmax);
iterate_byptr_dynamic_length(grown_partlist + 0 grown_partmax);
iterate_byptr_dynamic_length(安培; grown_partlist [0],grown_partmax);
}
有两种基本方法,通过数组interate:按指数
和通过指针
。它的不的事数组的定义方式(如全局/静态
- > INT myary [37];
或通过的malloc / realloc的
- > 为int * myptr =的malloc(sizeof的(INT)* 37);
)。 按指数语法和的指针语法是可以互换的。如果你想在12元,下面都是等效的:
myary [12]
*(myary + 12)
*(&放大器; myary [12])myptr [12]
*(myptr + 12)
*(&放大器; myptr [12])
这就是为什么所有上述会产生相同的结果。
I am having an issue creating a dynamic array of structures. I have seen and tried to implement a few examples on here and other sites, the examples as well as how they allocate memory tend to differ, and I can't seem to get any of them to work for me. Any help would be greatly appreciated.
typedef struct node {
int index;
int xmin, xmax, ymin, ymax;
} partition;
partition* part1 = (partition *)malloc(sizeof(partition) * 50);
I can't even get this right. It gives me the following error: error: initializer element is not constant
If anyone could explain how something like this should be implemented I would greatly appreciate it.
Also, once I have that part down, how would I add values into the elements of the structure? Would something like the below work?
part1[i]->index = x;
The compiler is complaining because you're doing:
partition* part1 = (partition *)malloc(sizeof(partition) * 50);
Do this instead:
partition* part1;
int
main(void)
{
part1 = (partition *)malloc(sizeof(partition) * 50);
...
}
Your version used an initializer on a global, which in C must be a constant value. By moving the malloc into a function, you are "initializing the value" with your code, but you aren't using an initializer as defined in the language.
Likewise, you could have had a global that was initialized:
int twenty_two = 22;
Here 22
is a constant and thus allowable.
UPDATE: Here's a somewhat lengthy example that will show most of the possible ways:
#define PARTMAX 50
partition static_partlist[PARTMAX];
partition *dynamic_partlist;
int grown_partmax;
partition *grown_partlist;
void
iterate_byindex_static_length(partition *partlist)
{
int idx;
for (idx = 0; idx < PARTMAX; ++idx)
do_something(&partlist[idx]);
}
void
iterate_byptr_static_length(partition *partlist)
{
partition *cur;
partition *end;
// these are all equivalent:
// end = partlist + PARTMAX;
// end = &partlist[PARTMAX];
end = partlist + PARTMAX;
for (cur = partlist; cur < end; ++cur)
do_something(cur);
}
void
iterate_byindex_dynamic_length(partition *partlist,int partmax)
{
int idx;
for (idx = 0; idx < partmax; ++idx)
do_something(&partlist[idx]);
}
void
iterate_byptr_dynamic_length(partition *partlist,int partmax)
{
partition *cur;
partition *end;
// these are all equivalent:
// end = partlist + partmax;
// end = &partlist[partmax];
end = partlist + partmax;
for (cur = partlist; cur < end; ++cur)
do_something(cur);
}
int
main(void)
{
partition *part;
dynamic_partlist = malloc(sizeof(partition) * PARTMAX);
// these are all the same
iterate_byindex_static_length(dynamic_partlist);
iterate_byindex_static_length(dynamic_partlist + 0);
iterate_byindex_static_length(&dynamic_partlist[0]);
// as are these
iterate_byptr_static_length(static_partlist);
iterate_byptr_static_length(static_partlist + 0);
iterate_byptr_static_length(&static_partlist[0]);
// still the same ...
iterate_byindex_dynamic_length(dynamic_partlist,PARTMAX);
iterate_byindex_dynamic_length(dynamic_partlist + 0,PARTMAX);
iterate_byindex_dynamic_length(&dynamic_partlist[0],PARTMAX);
// yet again the same ...
iterate_byptr_dynamic_length(static_partlist,PARTMAX);
iterate_byptr_dynamic_length(static_partlist + 0,PARTMAX);
iterate_byptr_dynamic_length(&static_partlist[0],PARTMAX);
// let's grow an array dynamically and fill it ...
for (idx = 0; idx < 10; ++idx) {
// grow the list -- Note that realloc is smart enough to handle
// the fact that grown_partlist is NULL on the first time through
++grown_partmax;
grown_partlist = realloc(grown_partlist,
grown_partmax * sizeof(partition));
part = &grown_partlist[grown_partmax - 1];
// fill in part with whatever data ...
}
// once again, still the same
iterate_byindex_dynamic_length(grown_partlist,grown_partmax);
iterate_byindex_dynamic_length(grown_partlist + 0,grown_partmax);
iterate_byindex_dynamic_length(&grown_partlist[0],grown_partmax);
// sheesh, do things ever change? :-)
iterate_byptr_dynamic_length(grown_partlist,grown_partmax);
iterate_byptr_dynamic_length(grown_partlist + 0,grown_partmax);
iterate_byptr_dynamic_length(&grown_partlist[0],grown_partmax);
}
There are two basic ways to interate through an array: by index
and by pointer
. It does not matter how the array was defined (e.g. global/static
--> int myary[37];
or via malloc/realloc
--> int *myptr = malloc(sizeof(int) * 37);
). The "by index" syntax and "by pointer" syntaxes are interchangeable. If you wanted the 12th element, the following are all equivalent:
myary[12]
*(myary + 12)
*(&myary[12])
myptr[12]
*(myptr + 12)
*(&myptr[12])
That's why all of the above will produce the same results.
这篇关于颁布实施的结构动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!