error: too many initializers for 'int [0][5]'

What am I doing wrong?

struct s {
  
  char c;
  int i;
  int pattern[][5];
  
};

struct s a = {'a', 5, {{1, 1, 1, 1, 0}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {1, 1, 1, 1, 0}}};

yields: error: too many initializers for 'int [0][5]'

Regards /Pelle

What am I doing wrong?

You're using sixteen bits where one would do.

Or did you mean why am I getting the error message?

Well... couldn't you have answered both?

  int pattern[][5];

The size of an array can be omitted only when the size can be inferred from the number of initializers present. There being no initializers, the first size of the array is 0.

error: too many initializers for 'int [0][5]'

See, the compiler told you that.

How many initializers did you define when you tried to initialize the instance? More than 0? Yes. That’s too many.

What’s so hard to understand about that?

Good point - gcc makes no complaint. Just one of the differences between C and C++, I guess

PaulS:   int pattern[][5];

The size of an array can be omitted only when the size can be inferred from the number of initializers present. There being no initializers, the first size of the array is 0.

error: too many initializers for 'int [0][5]'

See, the compiler told you that.

How many initializers did you define when you tried to initialize the instance? More than 0? Yes. That's too many.

What's so hard to understand about that?

Ok, true... My bad for posting a poor question. What I really wanted was that array to be able to take something with like 7 elements as well.

So applying the fix int pattern[5][5]; just won't cut the cheese.

struct s {
  
  char c;
  int i;
  int pattern[][5];
  
};

struct s a = {'a', 5, {{1, 1, 1, 1, 0}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {1, 1, 1, 1, 0}}};
struct s b = {'b', 7, {{1, 1, 1, 1, 0}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {1, 1, 1, 1, 0}, {1, 1, 1, 1, 0}, {1, 1, 1, 1, 0}}};

So how can I accomplish what I intend by the code above?

/Pelle

To put it simply, you shouldn’t be doing what you are trying to do. Stick to predefined array sizes within your structs. Even when used properly, flexible array members are considered poor programming practice, and using them properly requires dynamically allocating your structs.

Applying the fix would be:

struct s {
  
  char c;
  int i;
  int pattern[5][5];
  
};

Yes, that means the struct is hard coded with a 5x5 pattern array, but unless you fully understand how to use flexible array members you shouldn’t use them.

Hang in there @pelle76. My apologies that developers who understand what is going on sometimes tend to be short with folks who are trying to learn.

ptdecker: Hang in there @pelle76. My apologies that developers who understand what is going on sometimes tend to be short with folks who are trying to learn.

Hang in for another 4 years? You are replying to a post made in 2011.

This is the second dead thread he has revived today. Must be a nostalgia buff. Or a spammer biding his time.

Pete

Yeah, but check it out: half of his posts got Karma points!!!

So his mother's registered too.

Pete

Holy Cow! 100% karma points! I don't think I've ever seen that before. Betcha he goes over 100%!