简体   繁体   中英

what is relation of "int a=5" with scanf("%d",ptr) if [ "int* ptr"] ptr is pointer?

code

int *ptr;
int a=10;
scanf("%d",ptr);
printf("%d",*ptr);

output:-
5 //for scanf
5 //from printf

but

int *ptr;
// removing this `int a=10;`
scanf("%d",ptr);
printf("%d",*ptr);

output change :-
5 //for scanf
  //blank (nothing from printf)

and here I was trying to scanf value to pointer directly so how can I scanf value to ptr

without

int a,*ptr;
ptr = &a;

is it compulsory to assign the address of another variable to ptr before putting(scanf) value in it

The answer to your question is "yes," it's necessary to assign a pointer to something before using it for pretty much anything (other than merely storing the pointer location). Point your pointer to the variable you use scanf() on. The code you titled "without" seems to be what you need.

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