Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'+=' : pointer on left; needs integral value on right

Tags:

c++

c

I am confused. I can not use this on a float? Must it be a integer? I try to define that as a point but I guess I can not convert float to float *

//global definition
float g_posX = 0.0f;

&g_posX -= 3.03f;
like image 620
numerical25 Avatar asked Feb 17 '26 06:02

numerical25


2 Answers

You probably simply want to do this:

float g_posX = 0.0f;
g_posX -= 3.03f;

What your code tries to do is take the address of g_posX and subtract 3.03f from the address. That does not work, for two reasons:

  • The address is not an lvalue: it cannot be assigned to. Assigning to an address would be meaningless. What would it do, move the variable around in memory?
  • Pointer arithmetic can only be done using integers. Fractional addresses do not exist.
like image 155
Thomas Avatar answered Feb 19 '26 18:02

Thomas


If you want to subtract from the float then just name the variable and don't take its address:

g_posX -= 3.03f;

Otherwise, &g_posX is an rvalue that you can't assign to it anything.

like image 40
Khaled Alshaya Avatar answered Feb 19 '26 20:02

Khaled Alshaya



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!