Casting Error: lvalue required as left operand of assignment

你说的曾经没有我的故事 提交于 2019-12-02 02:44:12

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