实现动态结构数组的问题

时间:2015-10-27 02:01:37

标签: c

我在创建动态结构数组时遇到了问题。我已经看到并尝试在这里和其他网站上实现一些示例,示例以及它们如何分配内存往往会有所不同,我似乎无法让它们中的任何一个为我工作。任何帮助将不胜感激。

typedef struct node {
    int index;
    int xmin, xmax, ymin, ymax;
} partition;

partition* part1 = (partition *)malloc(sizeof(partition) * 50);

我甚至无法做到这一点。它给了我以下错误: 错误:初始化元素不是常量

如果有人能解释如何实施这样的事情,我将非常感激。

此外,一旦我将该部分放下,我将如何将值添加到结构的元素中?会有类似下面的工作吗?

part1[i]->index = x;

1 个答案:

答案 0 :(得分:2)

编译器正在抱怨,因为你正在做:

partition* part1 = (partition *)malloc(sizeof(partition) * 50);

请改为:

partition* part1;

int
main(void)
{

    part1 = (partition *)malloc(sizeof(partition) * 50);

    ...
}

您的版本在全局上使用初始化程序,在C中必须是常量值。通过将malloc移动到函数中,您可以使用代码“初始化值”,但是您没有使用语言中定义的初始化程序

同样,你可能有一个 初始化的全局:

int twenty_two = 22;

这里22是常数,因此是允许的。

更新:这是一个有点冗长的例子,它将展示大多数可能的方式:

#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);
}

通过数组进行交互有两种基本方法:by indexby pointer。它关于如何定义数组(例如global/static - &gt; int myary[37];或通过malloc/realloc - &gt; int *myptr = malloc(sizeof(int) * 37);)。 “by index”语法和“by pointer”语法是可互换的。如果您想要第12个元素,则以下内容都是等效的:

myary[12]
*(myary + 12)
*(&myary[12])

myptr[12]
*(myptr + 12)
*(&myptr[12])

这就是为什么上述所有内容都会产生相同的结果。