So I'm trying to use ether_aton()
which returns a struct ether_addr *
.
I'm trying to put this in my struct ether_header *eptr
(from net/ethernet.h) which has the ether_shost
member. I tried this:
struct ether_header *eptr; /* net/ethernet.h */
...
(struct ether_addr*)(eptr->ether_shost) = ether_aton(SRC_ETHER_ADDR);
This gives me the "Error: lvalue required as left operand of assignment"
I know I'm just not casting it correctly, but can't figure out how.
EDIT: Ended up getting it. Thanks for the help guys.
struct ether_addr* eth_addr = ether_aton(SRC_ETHER_ADDR);
int i;
for(i=0; i<6; i++)
eptr->ether_shost[i] = eth_addr->ether_addr_octet[i];
Just had to assign each of the octet individually.
You cannot cast the left operand of the assignment operator in C.
One poster wrote " You cannot cast the left operand of an assignment operator in C"
This is true, sort of, but there is one case where it is not true: casting, then dereferencing a pointer:
*((int *) chrPtrValue) = some_integer_expression;
In this case, we are telling the compiler to consider chrPtrValue to be an integer pointer expression, then we ask the compiler to dereference it so we can store something there.
Notice that you can do some funky/dangerous pointer operations this way, such as:
*((int *) 0xFEDBCA01) = 0;
Store a zero in an arbitrary location just by providing a literal constant value! Crazy, but useful if you are peeking and poking memory-mapped I/O registers, for example.
More specifically, you're "casting" ether_shost (a struct). I think you meant to cast "eptr".
This should compile (but it's meaningless and silly):
(struct ether_addr*)(eptr)->ether_shost = ether_aton(SRC_ETHER_ADDR);
来源:https://stackoverflow.com/questions/11057728/casting-error-lvalue-required-as-left-operand-of-assignment