Casting Error: lvalue required as left operand of assignment

空扰寡人 提交于 2019-12-02 03:41:16

问题


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.


回答1:


You cannot cast the left operand of the assignment operator in C.




回答2:


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.




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!