简体   繁体   English

C语言中的结构指针数组,初始化程序错误无效

[英]Array of struct pointers, invalid initializer error, in C

This code: 这段代码:

extern void *malloc(unsigned int);
struct Box {
    int x, y ,w, h;
};

struct Wall {
    char color[15];
    struct Box *boxes[20];
};

int main(int argc, const char *argv[])
{
    struct Wall *mywall = malloc(sizeof(struct Wall));
    struct Box *myboxes[] = mywall->boxes;
    return 0;
}

gives me invalid initializer error at line 14. What I am trying to do, is to get a copy of array of struct pointers, which are in a different struct. 在第14行给出了invalid initializer错误。我想要做的是获取结构指针数组的副本,它们位于不同的结构中。

Ouch; 哎哟; there are a number of problems here. 这里有很多问题。

extern void *malloc(unsigned int);

Don't do that; 不要那样做; use #include <stdlib.h> because that will be correct and what you wrote is typically incorrect (the argument to malloc() is a size_t , which is not necessarily an unsigned int ; it might be unsigned long , or some other type). 使用#include <stdlib.h>因为这是正确的,你写的内容通常是不正确的( malloc()的参数是size_t ,它不一定是unsigned int ;它可能是unsigned long ,或者是其他类型) 。

struct Box {
    int x, y ,w, h;
};

Apart from erratic space, struct Box is OK. 除了不稳定的空间, struct Box还可以。

struct Wall {
    char color[15];
    struct Box *boxes[20];
};

And struct Wall is OK too. struct Wall也可以。

int main(int argc, const char *argv[])

You aren't using argc or argv , so you'd be better using the alternative declaration of: 您没有使用argcargv ,因此您最好使用以下替代声明:

int main(void)

Original code again: 原始代码:

{
    struct Wall *mywall = malloc(sizeof(struct Wall));

This allocates but does not initialize a single struct Wall . 这会分配但不会初始化单个struct Wall Of itself, it is OK, though you should check that the allocation succeeded before you use it. 就其本身而言,虽然您应该在使用之前检查分配是否成功。 You also need to worry about allocating the struct Box items that the elements of the array will point to. 您还需要担心分配数组元素将指向的struct Box项。

    struct Box *myboxes[] = mywall->boxes;

You've got a minor catastrophe on hand here. 你手边有一场小小的灾难。 You can't copy arrays like that. 你无法复制那样的数组。 You haven't checked that you've got an array. 你没有检查过你有一个阵列。 Ignoring the error checking, you are stuck with one of: 忽略错误检查,你会遇到以下问题之一:

    struct Box *myboxes[] = { &mywall->boxes[0], &mywall->boxes[1], ... };

or: 要么:

    struct Box **myboxes = &mywall->boxes;

I'm not convinced that you'd want the second version, for all it's shorter. 我不相信你想要第二个版本,因为它更短。

    return 0;

I like to see return 0; 我喜欢看到return 0; at the end of main() , even though C99 allows you to omit it. main()的末尾,即使C99允许你省略它。

}

How about: 怎么样:

struct Box **myboxes = mywall->boxes;

?

Then you can do stuff like: 然后你可以做的事情:

 for ( int i = 0 ; i < 15 ; i++ )
    mywall->boxes[i] = malloc(sizeof(Box));
 Box* x = myboxes[1];

As the code is now, mywall->boxes isn't initialized. 由于代码现在, mywall->boxes未初始化。

NOTE: just re-read the question - this won't return a copy of the array, but point to the same location. 注意:只需重新阅读问题 - 这不会返回数组的副本,而是指向同一位置。 There's no short solution for a copy without using memcpy or just copying the structs. 没有使用memcpy或只是复制结构的副本没有简短的解决方案。

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

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