Annoying C compilation error

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
I have the program:

static int var1;
static int var2;

static void f1(void){

int local_var1;

local_var1 = var2 - var1;

return;
}

I get a compilation error:
Serious error: C2953E: Illegal in the context of an l-value: 'unary -'

and it refers to the line "local_var1 = var2 - var1;"

Any ideas what's going on?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
No idea, it works fine using gcc here. Did you set the static vars to something before calling the function?
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Oops forgot to put that info. It's an ARM compiler.

I checked the docs and it says:
These errors can occur when a non-constant expression is being assigned to a static object. ANSI C requires the initializer for a static object to be a constant expression.

I'm not really sure how it applies to this situation.
 

onelin

Senior member
Dec 11, 2001
874
0
0
Check if it still happens if you make it var2 - 5; // (test if the constant thing is true?)
It's kind of odd to call it a unary minus, you could also try (var2 - var1) to see if grouping them helps it figure it out or not.

Sounds like a C compiler (in which case, $$$ for ARM C), so that's a mystery...if it was assembly it might be more clear cut.

the problem may just be the statics, though...is there any reason in particular for them? (and as nothinman said, are they assigned values first?)
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Thanks for the help so far, but I'm still not sure what it is.

onelin0, I tried your suggestions, but nothing new.

I wasn't quite sure what an l-value was so I looked it up.

I was thinking that (for some odd reason), it was substracting the l-values of var2-var1 instead of the r-values of var2-var1. I have no idea why that would happen!

I used the static keyword for scoping purposes pretty much. I don't want other files outside to call this function or use this variable. And yes, I tried giving them initial values, but the compiler still failed at the same place.

argh!
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
n/m I'm an idiot.

There was an error in the previous lines (not shown in the sample code) that I didn't see.

The above sample code should compile in any compiler.