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
Well, casting performs a type conversion. In general case type conversion is a non-trivial operation, which completely changes the representation of the value. Under these circumstances it should be painfully obvious that the result of any conversion is cannot possibly be an lvalue.
For example, if you have an int i = 0;
variable, you can convert it to type double
as (double) i
. How can you possibly expect the result of this conversion to be an lvalue? I mean, it just doesn't make any sense. You apparently expect to be able to do (double) i = 3.0;
... Or double *p = &(double) i;
So, what should happen to the value of i
in the former example, considering that type double
might not even have the same size as type int
? And even if they had the same size, what would you expect to happen?
Your assumption about all pointers having the same size is incorrect. In C language in general case (with few exceptions) different pointer types have different sizes, different internal representations and different alignment requirements. Even if they were guaranteed to have the same representation, I still don't see why pointers should be separated from all other types and given special treatment in explicit cast situations.
Finally, what you seem to be advocating here is that your original conversion should perform a raw-memory reinterpretation of one pointer type as another pointer type. Raw-memory reinterpretation in almost all cases is a hack. Why this hack should be elevated to the level of the language feature is entirely not clear to me.
Since it is a hack, performing such reinterpretations should require a conscious effort from the user. If you want to perform it in your example, you should do *(type_t **) &x
, which will indeed reinterpret x
as an lvalue of type type_t *
. But allowing the same thing through a mere (type_t *) x
would be a disaster completely disconnected with the design principles of C language.