lvalue required as left operand of assignment
Hi all, it's been a long time since I did coding in C, but thought to pick up a very old project again, just to show off what I have been working on ten years ago.
I deducted my problem as follows: Code:
#include <stdio.h> Quote:
Code:
SET_BIT(bar->act,bit3); Why is it saying that bar->act is not a valid lvalue while both bar->act and the bit are cast to (long long)? |
funnily enough:
if I change Code:
SET_BIT(bar->act,bit3); Code:
bar->act |= bit3; Code:
(long long}bar->act |= (long long)bit3 Ideally, I'd rewrite the SET_BIT macro so that it will be accepted again, so that I don't have to go through thousands of lines of code to fix this... Edit: It would seem obvious to remove the cast from the SET_BIT macro; However, I'd have to analyze and see how and why. Naturally, I'm a lazy person as far as this goes, so what I'm curious about is why the cast appears to be invalid. (It's fairly possible that this SET_BIT is used elsewhere on a char, or maybe even a float or double... again: It will take a fair amount of going hence and forth in the code.) So: how could I safely typecast both the var and bit so that either is accepted as lvalue and rvalue respectively? |
Hey Ramurd,
great that you got back to C programming :-) Regarding your problem, this error message is correct and as expected. If you look into the C99 standard, you'll find the following section about cast expressions: Quote:
Andi |
Hey Forza,
Thanks; It's pretty obvious when I stopped actively coding ;-) That was around 1999, hence I did not get that standard. Thanks for pointing out that I should look at the standards. While not the answer I hoped for, (I guess I'll have to try and see if removing the casts will result in big issues), I'm glad you pointed it out. Back to programming... (oh, and I noticed I "forgot" to free(bar) in the quick-and-dirty example... :-) |
I'm not sure what you are doing at a physical layer, but if you can break bits into a byte and throw them into an array you could use a ptr to an address.
I think your trying to OR the bits together to get some datatype In this example 'payload' is the array. Code:
for ( int inc = 0; inc < theSize3; inc++ ) |
Code:
#define SET_BIT(var,bit) do { ((*(unsigned long long*) &(var) |= ((unsigned long long)1 << (bit))); } while(0) |
Thanks all for giving some neat solutions and answers; should've come up with tuxdev's approach;
For now I ended up with: Code:
#define SET_BIT(var,bit) ((*(long long*) &(var) |= ((long long)(bit)))) So what I can do now is this: Code:
#include <stdlib.h> Quote:
Code:
#define IS_SET(flag,bit) (flag & bit) The usage is in terms of Code:
#define RECORD_DIRTY 1 I tagged the helpful posts as I noticed they helped me find a good solution, and quite happy that tuxdev showed how to work around the lvalue limitation regarding type casts. And marked the thread as solved. I guess this is material one does not easily figures out without some (fresh) hands-on experience. :-) |
All times are GMT -5. The time now is 08:47 AM. |