Casting a pointer does not produce an lvalue. Why?

后端 未结 8 1456
孤独总比滥情好
孤独总比滥情好 2020-11-30 12:25

After posting one of my most controversial answers here, I dare to ask a few questions and eventually fill some gaps in my knowledge.

Why isn\'t an expression of the

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 13:22

    From a top level, it would generally serve no purpose. Instead of '((type_t *) x) = ' one might as well go ahead and do 'x = ', assuming x is a pointer in your example. If one wishes to directly modify values pointed by the address 'x' but at the same time after interpreting it to be a pointer to a new data type then *((type_t **) &x) = is the way forward. Again ((type_t **) &x) = would serve no purpose, let alone the fact it is not a valid lvalue.

    Also in cases of ((int *)x)++, where at least 'gcc' does not complain along the lines of 'lvalue' it could be reinterpreting it as 'x = (int *)x + 1'

提交回复
热议问题