0

Good day everyone. I'm working on this Game of Life problem and trying to pass an array of boolean:(Given)

game({0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, 15, 9);

into the function below:(Given)

void game(bool cells[], int size, int num_gen) 

However, I'm getting this error: Too many initializer values C/C++ (146)

I tried to play around and was able to pass in values by declaring the array in the main function:

int main(void) 
{
    int cells[]={0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
}

But this is my school assignment, so I'm not supposed to edit any given codes/inputs/functions.

I'm kinda lost now, will appreciate any inputs. Thanks in advance!

6
  • 3
    But this is my school assignment -- What computer language is this assignment supposed to be written in? Is it C, or is it C++? Commented Sep 23, 2020 at 14:36
  • Which part exactly are you not allowed to edit? Because you will need to change either the call or the declaration Commented Sep 23, 2020 at 14:36
  • @PaulMcKenzie Technically it should be in C Commented Sep 23, 2020 at 14:45
  • @UnholySheep Both the input (test case) and game function were given. So we shouldn't modify those Commented Sep 23, 2020 at 14:45
  • So you have been given code that doesn't compile and are supposed to make it compile without changing anything? That is going to be difficult Commented Sep 23, 2020 at 14:49

3 Answers 3

1
void game(bool cells[], int size, int num_gen) 

The first parameter of your function is not an array. Sure, you declared the parameter to be an array of unspecified length, but such parameter will be adjusted to be a pointer to element of such array. A function parameter is never an array in C++.

As such, you are attempting to initialise a pointer with a brace-enclosed list of values. There are more than one value, so it's wrong (and the types of the values are also incompatible with a pointer).

You can create an array, and pass a pointer to element of that array:

int cells[]={0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
game(cells, std::size(cells), 9);
Sign up to request clarification or add additional context in comments.

Comments

0

A list of values in braces is not valid at an expression. What you need is a compound literal:

game((bool []){0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, 15, 9);

The syntax that looks like a cast before the braced segment gives you the type. You can also use this syntax for a struct literal.

1 Comment

Note that this is C-only. C++ doesn't have compound literals. It's a shame that the OP has both tags.
0

You can use compound literal for this.

Something like

game((bool []){0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, 15, 9);

2 Comments

Note that this is C-only. C++ doesn't have compound literals. It's a shame that the OP has both tags.
@Ruslan Oops my bad. It should be in C, but I'll also like to know what can be done if it's in C++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.