Pointers again... what is wrong with this?

1
2
3
4
5
6
7
void RandomArray(vector<int>* getalletjes, int grootte) 
{
	for (int i = 0; i < grootte; i++)
	{
		*getalletjes[i] = rand() % 101;
	}
}


Function call:
RandomArray(&getallen, input);

It gives this error:
1
2
3
4
5
6
7
8
9
10
11
12
error C2100: illegal indirection
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1>        d:\program files\microsoft visual studio 9.0\vc\include\vector(562): could be 'std::vector<_Ty> &std::vector<_Ty>::operator =(const std::vector<_Ty> &)'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>        while trying to match the argument list '(std::vector<_Ty>, int)'
1>        with
1>        [
1>            _Ty=int
1>        ]


If I press "continue", the program works fine though. What's wrong with this?

Thanks :)
Last edited on
This line
*getalletjes[i] = rand() % 101;
should be this
(*getalletjes)[i] = rand() % 101;

Also, if you want numbers from 1 to 100, it should be this
(*getalletjes)[i] = (rand() % 100) + 1;

Also, if the vector does not already have 10 elements allocated, it should be this
getalletjes->push_back( (rand() % 100) + 1 );

Thank you, that works fine.

Why does it needs (*getalletjes)[i] and so why doesn't my way work?
Why pass pointers? Are you sure that getalletjes has enough space allocated? This is easier on the eyes and safer:

1
2
3
4
5
6
7
8
9
10
11
12
typedef vector<int> Getallen;

void RandomArray(Getallen& getalletjes, int grootte) 
{
    getalletjes.clear();
    for (int i = 0; i < grootte; i++)
    {
        getalletjes.push_back(rand() % 101);
    }
}

RandomArray(getallen, input);



I love reading Dutch. :-)
Hehe... "getalletjes" was a fast name as I was out of inspiration :D

Can you please explain the use of push_back? Thanks :)
Well, push_back() is used to put something on the end of a vector. It will grow the vector as needed. The vector getallen can be created without a size specifier and grow as needed.

If you don't use push_back(), your function would need to verify that grootte <= getalletjes.size(). Otherwise you risk assigning past the end of the vector.
Okay, thank you very much for the explaination! :)
"Why does it needs (*getalletjes)[i] and so why doesn't my way work?"

It needs (*getalletjes)[i] instead of *getalletjes[i] because the first option: (*getalletjes)[i] finds where getalletjes was pointing to (which alas is a vector), and then chooses the i'th index of this vector. The way you did it: *getalletjes[i] tries to access the i'th element of the pointer (which makes no sense)...and then dereference that. If this were an array, what you did would be fine because an array variable is really just a pointer to the first element of the array...but for a vector, a pointer to a vector object points to the actual vector object itself, so you need to dereference the vector first...I hope that made sense =)

As mentioned above, a way around this confusion is simply to pass the vector by reference, then you can access it like usual.

Topic archived. No new replies allowed.