Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert unsigned int *' to 'unsigned int *&

Tags:

c++

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,

like image 521
Yakov Avatar asked Nov 22 '25 16:11

Yakov


2 Answers

&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

like image 117
Andrew Avatar answered Nov 25 '25 06:11

Andrew


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);
like image 40
eq- Avatar answered Nov 25 '25 05:11

eq-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!