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.

4
  • 1
    You cannot copy or assign arrays in C. (However, you can wrap them up into a struct, which can be assigned.) Commented Mar 14, 2012 at 19:02
  • 4
    Why not #include <stdlib.h> instead of extern void *malloc(unsigned int); ? Commented Mar 14, 2012 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. Commented Mar 14, 2012 at 19:06
  • Does this answer your question? "Returning an array of char pointers", "Assign to 2D array a 2D array in a struct" Commented Sep 17, 2022 at 19:46

2 Answers 2

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.

} 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.