【发布时间】:2018-10-11 06:21:36
【问题描述】:
void main()
{
int a;
printf("Enter a value to store :");
scanf("%d",&a) ; // Why Can't we use a instead of &a to store value of variable a ???
printf("value of a is %d", a) ;
printf("\naddress of a is %d", &a) ;
getch() ;
}
【问题讨论】:
-
欢迎来到 stackoverflow.com。请花一些时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请take the tour 和read about how to ask good questions。最后请阅读this question checklist。
-
了解函数和指针传递的原因。然后它就会变得明显。
-
至于您的“问题”(您确实应该在代码 sn-ps 之外单独且清楚地编写),C 在将参数传递给函数时只有 按值传递。这意味着值被复制,函数内的参数与调用函数时使用的原始表达式完全无关。
scanf正在做的是 emulating 通过引用传递。 -
int a[1]; scanf("%d", a); printf("you entered %d.\n", *a);
标签: c user-input