Int I = 5,* p; P =&安培; I; scanf(“%d”,p); printf("%d%d \ n",I,* p); [英] Int I=5, *p; p=&I; scanf("%d", p); printf("%d %d\n", I, *p);

查看:107
本文介绍了Int I = 5,* p; P =&安培; I; scanf(“%d”,p); printf("%d%d \ n",I,* p);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里p是一个指向i的指针,但是如果接受它,它如何通过scanf接受它应该保存给定数据的值。但它持有我的地址。打印* p和i时。我们得到运行时输入作为输出。

here p is a pointer pointing to i.but how it is able to accept the value through scanf if accepted it should hold the value of given data. but it is holding the address of i. when printing the *p and i. we get the run time input as output.

Int I=5, *p;
p=&I; scanf("%d", p);
printf("%d %d\n", I, *p);





我是什么尝试过:



是的,我已经尝试预测输出但是我执行了代码并且它正在工作并且我得到结果作为运行时输入。



What I have tried:

yes, I have tried to predict the output but I executed the code and it is working and Im getting the results as run time input.

推荐答案

p 是指向 I 的指针和 scanf 需要一个指针n命令才能知道它所读取的值的位置。

这是正常的,因为在C中一切都通过ta函数按值而不是按引用 - 这意味着该函数获取变量的副本而不是变量本身。如果你对它很瘦,那就很有意义:

p is a pointer to I and scanf needs a pointer n order to know where to put the value it reads.
This is normal, because in C everything is passed t a function by value not by reference - which means that the function gets a copy of the variable rather than the variable itself. That makes a lot of sense if you thin about it:
void foo(int x)
   {
   x = x +1;
   printf("%d\n");
   }

很好,但如果你这样叫它会发生什么:

Is fine, but what would happen if you called it like this:

foo(666);

如果您没有获取该值的副本,则会让代码尝试更改常数的值!



所以如果你想要从被调用的函数中改变调用函数中的某些东西,你必须传递一个指针而不是值,所以指针的副本是通过的,它指向与原始函数相同的位置。

If you didn't take a copy of the value, you would have the code trying to change the value of a constant number!

So if you want to change something in the calling function from the called function, you have to pass a pointer rather than the value, so a copy of the pointer is passes which points at the same place as the original.


这篇关于Int I = 5,* p; P =&安培; I; scanf(“%d”,p); printf("%d%d \ n",I,* p);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆