I have the following variables:
char *p;
int l=65;
Why do the following casts fail?
(int *)p=&l;
and:
The correct way to do this would be:
int I = 65;
char* p = (char*)&I;
&I
gives you an int*
that points to I
; you then cast this to a char*
and assign it to p
.
Note that you shouldn't ordinarily cast between pointers of unrelated types. A char*
can be used to access any object, though, so in this particular case it is safe.