• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Annoying C compilation error

AgentEL

Golden Member
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?
 
No idea, it works fine using gcc here. Did you set the static vars to something before calling the function?
 
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.
 
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?)
 
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!
 
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.
 
Back
Top