简体   繁体   中英

How do I assign a pointer to a variable?

Suppose we have a variable k which is equal 7:

int k=7;
int t=&k;

But this does not work. What's the mistake?

You probably meant:

int k=7;
int *t=&k;

&k takes the address of k . You probably mean

int *t = &k;

I have a good read for you: Alf P. Steinbach's pointer tutorial .

You declare t as of type int and try to assign a value of type int* . int* cannot implicitely cast to type int which leads to the error you are observing. The solution is simple: declar t as int* . However, it seems you have no deeper understanding of pointers so you should fix that first before trying anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM