I have the following variables:
char *p;
int l=65;
Why do the following casts fail?
(int *)p=&l;
and:
p=&((char) l);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The result of type conversion is always an rvalue. Rvalue cannot be assigned to, which is why your first expression doesn’t compile. Rvalue cannot be taken address of, which is why your second expression doesn’t compile.
In order to perform the correct type conversion, you have to to it as follows
This is the proper way to do what you tried to do in your second expression. It converts
int *pointer tochar *type.Your first expression is beyond repair. You can do
but what it does in the end is not really a conversion, but rather reinterpretation of the memory occupied by
char *pointer asint *pointer. It is an ugly illegal hack that most of the time has very little practical value.