5

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.

yasar
  • 13,158
  • 28
  • 95
  • 160
  • 1
    You cannot copy or assign arrays in C. (However, you can wrap them up into a struct, which *can* be assigned.) – Kerrek SB Mar 14 '12 at 19:02
  • 4
    Why not `#include ` instead of `extern void *malloc(unsigned int); `? – glglgl Mar 14 '12 at 19:03
  • 1
    @glglgl no particular reason. I wrote this piece of code just to demonstrate my point. I forgot where was malloc, and was too lazy to look for it. – yasar Mar 14 '12 at 19:06
  • Does this answer your question? "[Returning an array of char pointers](/q/2118218/90527)", "[Assign to 2D array a 2D array in a struct](/q/6770507/90527)" – outis Sep 17 '22 at 19:46

2 Answers2

6

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).

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

Apart from erratic space, struct Box is OK.

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

And struct Wall is OK too.

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

You aren't using argc or argv, so you'd be better using the alternative declaration of:

int main(void)

Original code again:

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

This allocates but does not initialize a single 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 *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; at the end of main(), even though C99 allows you to omit it.

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

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.

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625