I have a function which gets 'unsigned int *& argument
The parameter I want to transfer in my code is located in the std::vector<unsigned int> data
So,what I do is : I transfer the following parameter &data[0]
But get the compilation error:
unsigned int *' to 'unsigned int *&
What can be a work around? Thanks,
&data[0] is an rvalue and cannot be bound to non-const reference.
You can make it work this way:
unsigned int *ptr = &data[0];
func(ptr);
But possibly it's better to just change the signature of your function to
void foo(unsigned int &val); //or any other return type
There is a sense of passing a reference to a pointer in case you want to make a pointer point somewhere else. But I don't see a reason to do so in your case
The expression &data[0] yields indeed an r-value, which your function cannot accept.
A simple work-around if you don't want to alter your function (make sure you understand the reasons it requires a reference):
unsigned int* ptr = &data[0];
func(ptr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With