Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast an int's address to char pointer in C?

Currently the below code gives me a warning when i try to compile it:

int z; char *w;

w = &z;

How can i cast &z properly so that w stores the pointer to z's address?

like image 947
goe Avatar asked Oct 10 '09 19:10

goe


2 Answers

Try:

w = (char*) &z;
like image 127
csl Avatar answered Oct 02 '22 15:10

csl


Don't forget that generally casting pointers in C is a bad idea. Sure, there are some times when it is the right thing to do, but we can't see exactly what you're doing here. In most cases there is a better, more portable way to do whatever it is which you are aiming at here.

like image 41
Tim Avatar answered Oct 02 '22 15:10

Tim