I am curious if there is a way to pass an entire array or vector off to a function. I think that I read a little about it on the pointers or array tutorial, but I can't find it anymore.
Thanks,
David
Printable View
I am curious if there is a way to pass an entire array or vector off to a function. I think that I read a little about it on the pointers or array tutorial, but I can't find it anymore.
Thanks,
David
Pass an array as a pointer and pass a vector just the way you'd pass anything else.
Ok, thanks.
One more thing. When you pass a vector, do you need to specify what type it is, like <int> or <char>
Yes, you do. Or you can use a template and ignore the problem.
It is usually best to pass a vector by reference so the contents are not copied. If you don't plan on changing the vector in the function, then you should pass by const reference. So generally vectors are passed like this:Code:void foo(const std::vector<int>& my_vector)
Thanks, thats what I was looking for.
Ok, I can not get this thing to work for some reason.
If I use this:
The function is expecting an address, right?Code:const std::vector<int>& my_vector
Also it won't assign the address of the vector to my pointer, I'm using this:
Code:int *taken_ref;
taken_ref = & taken_pos;
No, if you use my version, the function is taking a reference, not an address ('&' has a different meaning in different uses). You just pass the vector as is (like any other variable) and the reference will be created automatically.
Well, that worked although I don't understand why. If it wouldn't too much trouble could you explain it?
Thanks
Do you know about references yet? Reading up on those would be a good start. Basically, a reference is like an alias. When you create a reference, you initialize it with another object, so in this code:you initialized r with i. Now both r and i refer to the same value, the value in i was not copied at all. From this point on in the program, you can do exactly the same thing with r that you can do with i, and vice versa. Anything you do to r applies to i as well. So these two lines of code will have the exact same effect:Code:int i = 3;
int& r = i;
Code:i = 5; // both i and r are now 5
or you can look at it this way:Code:r = 5; // both i and r are now 5
So when you make your function parameter a reference, the same thing occurs. The reference variable is initialized with the other variable (just as r is initialized with i above), and whatever you do to the reference variable applies to the original as well without any copies being made. Note that this is why the const can be important, because it protects you from accidentally changing the original vector.Code:int i = 1;
int& r = i;
++i; // value is now 2.
++r; // value is now 3.
++i; // value is now 4.
++r; // value is now 5.
std::cout << "i: " << i << '\n'; // i: 5
std::cout << "r: " << r << '\n'; // r: 5
Ok, I understand. I was just getting references confused with pointers. Thanks for taking your time to explain.
As a small aside to people just learning references and pointers, although most people call this a const reference, that's kind of a bad name as it's more correctly "a reference to const". For references, there is only one kind, so this nomenclature isn't ambiguous, but with pointers, a "const pointer" and "pointer to const" are very different.Quote:
Originally Posted by Daved