简体   繁体   English

使用“&”与printf()和scanf()一起使用

[英]The usage of “&” working with printf() and scanf()

I've a simple question concerning the correct usage of "&" before a variable. 关于在变量之前正确使用“&”,我有一个简单的问题。

printf( "34 * 7 =  " );
scanf( "%d", &x );
printf( "The solution is 238 \n");
printf( "Your solution is %d \n\n", x );

I don't understand why I must use "&x" when reading the user input and why I can go without when using the printf() function. 我不明白为什么在阅读用户输入时必须使用“&x”以及为什么在使用printf()函数时我可以不使用。 I would like to understand what is behind it. 我想了解背后的原因。

In C, C++, and Go, a prefix "&" is a unary operator denoting the address in memory of the argument, eg &x, &func, &a[3].

I don't understand why I must use "&x" when reading the user input 我不明白为什么在阅读用户输入时必须使用“&x”

Because that is how the language makers chose to define it. 因为这是语言制定者选择定义它的方式。 The & operator works fine even without printf and scanf . 即使没有printfscanf&运算符也能正常工作。

int *pointer_to_x = &x;

Now this scanf( "%d", pointer_to_x ); 现在这个scanf( "%d", pointer_to_x ); is same as scanf( "%d", &x ); scanf( "%d", &x );相同scanf( "%d", &x );

The scanf function takes a pointer to an int as an argument, in this case, while printf just takes the int. scanf函数将一个指向int的指针作为参数,在这种情况下,printf只接受int。 The & is the address-of operator. &是运营商的地址。

In printf() function, you are not changing value of variable ' x ', whereas in scanf() function you have to change the value of ' x '. printf()函数中,您不会更改变量' x '的值,而在scanf()函数中您必须更改' x '的值。 ' &x ' gives the address of x , this is passed to the function scanf() , and the value taken from user is stored in x using the address of x, finally when we return from function scanf() the value of x is stored with the value that user has given. “&X”给出x的地址,这被传递给函数scanf()的 ,以及从用户所采取的值使用x的地址存储在X,最后当我们从功能scanf()的x的值被存储回与用户给出的值。 If we do not use '&x', then scanf() function will get a local copy of variable ' x ' and the changes will not be reflected when we return from the function. 如果我们不使用'&x',那么scanf()函数将获得变量' x '的本地副本,并且当我们从函数返回时,不会反映更改。

In arguments are always passed by call by value method. 在参数中总是通过值调用方法传递。 So whatever argument you pass a copy is made and stored in the function. 因此无论您传递什么参数,副本都会被创建并存储在函数中。

But using pointers, we can make a function 'look-like' implementing a call by reference type of function call!. 但是使用指针,我们可以使函数“看起来像”通过函数调用的引用类型实现调用!

For eg: 例如:

int fun(int *p)
  {
    p=1;
    return p;
  }

Now take an example of a function call fun(&x); 现在举一个函数调用fun(&x);的例子fun(&x); , this function now changes the value of x to 1. ,此函数现在将x的值更改为1。

So in case of scanf() there should be an assignment happening inside the function call, and assigning a value to a variable out of scope of the function can only be done by knowing the address the variable(As the name has a scope). 因此,在scanf()情况下,应该在函数调用中发生赋值,并且只能通过知道变量的地址(因为名称具有范围)来为函数范围外的变量赋值。

So because of such reasons scanf() funciton is coded keeping in mind that the argument will be an address, so if you pass anything other than an address some of the code will obviously be wrong and thus results in an error. 因此,由于这样的原因, scanf()函数被编码,记住参数将是一个地址,所以如果你传递除地址以外的任何东西,一些代码显然会出错,从而导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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