Casting a pointer does not produce an lvalue. Why?

后端 未结 8 1408
孤独总比滥情好
孤独总比滥情好 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:21

    The result of a cast is never an lvalue by itself. But *((type_t *) x) is an lvalue.

    0 讨论(0)
  • 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'

    0 讨论(0)
提交回复
热议问题