Initializing array...whats wrong with it.

Trying to initialize array but i get an error no matter what i do. what am i doing wrong?
1
2
3
4
5
6
7
8
9
10
11
void bD (int n, int r, int b[])
{
    b[] = {1,0,0,1}; //this yields an error of "expected expression"
}

or

void bD (int n, int r, int b[6])
{
    b[6] = {1,0,0,1}; //this yields an error of " excess elements in scalar initializer"
}
Last edited on
What exactly are you trying to accomplish? Why are you trying to pass an array by value in your parameters. If you want to modify the array you should be passing it by reference. Also, you can only pass the address of the array, not the whole block of memory itself if that makes sense.

Check out this link, scroll down to "arrays as parameters"

http://www.cplusplus.com/doc/tutorial/arrays/
Actually he's passing the array correctly.
But it's not possible to do that operation when it's already constructed. You have to set each value one by one.
Also threre is no need to put 6 between the brackets in the second function ..
Last edited on
Exactly so what EssGeEich is saying is valid.

You need something like this:

1
2
3
4
5
6
7
void bD(int n, int r, int b[6])
{
   b[0] = 1;
   b[1] = 0;
   b[2] = 0;
   b[3] = 1;
}


If you are going to pass your array by value the way you've done..

Note: You could also just iterate through the array if you already knew the size and contents with a for loop.
Last edited on
I was trying to make a binary to decimal converter. I only need it to work for at most 6 binary number, hence the 6. Also thanks i got it.
Topic archived. No new replies allowed.