In C++ how to pass uint16_t variable to the function with an argument unsigned int &?

  • A+
Category:Languages

I have a variable uint16_t a=35; and I have a function

UINT Read(unsigned int& nVal); 

How do I pass a to Read() as unsigned int&?

If I pass like this

Read(a);  

I am getting the error below:

cannot convert parameter 1 from 'uint16_t' to 'unsigned int &

 


You will need to copy the value into a (named) temporary, call the function, and then copy the temp back (possibly after checking for overflow).

uint16_t a = 35; ... unsigned int temp = a; const unsigned int result = Read(temp); // check for overflow here a = temp; 

Of course, if you can change the definition of a to be unsigned int, then that is much more straightforward (but I assume that is not possible for other reasons).

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: