为什么scanf必须取操作符的地址
正如标题所说,我总是想知道为什么 scanf
必须采用 address of
运算符(&)。
As the title says, I always wonder why scanf
must take the address of
operator (&).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
因为 C 只有“按值传递”参数,所以要传递一个“变量”来放入值,您必须传递它的地址(或指向该变量的指针)。
Because C only has "pass-by-value" parameters, so to pass a 'variable' to put a value into, you have to pass its address (or a pointer to the variable).
scanf 不采用“运算符 (&) 的地址”。它需要一个指针。大多数情况下,指向输出变量的指针是通过在 scanf 调用中使用地址运算符来获取的,例如
但这不是唯一的方法。以下内容同样有效:
同样,我们可以使用以下代码执行相同的操作:
scanf does not take "the address of operator (&)". It takes a pointer. Most often the pointer to the output variable is gotten by using the address-of operator in the scanf call, e.g.
But that is not the only way to do it. The following is just as valid:
Likewise we can do the same thing with the following code:
因为它需要地址来放置它读取的值。如果将变量声明为指针,则
scanf
将不需要&
。Because it needs the address to place the value it reads. If you declare you variable as a pointer, the
scanf
will not need the&
.其他人都已经很好地描述了 sscanf 需要将其输出放在某个地方,但为什么不返回它呢?因为它必须返回很多东西 - 它可以填充多个变量(由格式驱动),并且它返回一个 int 指示它填充了多少个变量。
Everyone else has described well that sscanf needs to put its output somewhere, but why not return it? Becuase it has to return many things - it can fill in more than one variable (driven by the formatting) and it returns an int indicating how many of those variables it filled in.
当您使用标准输入设备(通常是键盘)输入内容时,传入的数据必须
存储
某处。您必须指向
内存中的某个位置,以便可以将数据存储在那里。要指向
内存位置,您需要该位置的地址
。因此,您必须使用&
运算符和scanf()
来传递变量的地址。When you input something with standard input device (usually keyboard), the data that comes through must be
stored
somewhere. You mustpoint
somewhere in the memory so that data can be stored there. Topoint
a memory location, you need theaddress
of that location. So you must pass your variable's address by using&
operator withscanf()
.因为该值将存储在内存地址中(在哪里?)。所以 scanf() 处理 (&) 运算符。
as the value is going to be stored,(where?), in the memory address. so scanf() deals with (&) operator.
它告诉在哪里写入输入值,因为 (&) 运算符的地址给出了变量的地址。因此,带有变量名和地址运算符的 scanf 意味着将值写入该位置。
您还可以使用 (&) 运算符的地址和十六进制格式的 %p 格式说明符检查任何变量的地址。
It tells where to write the input value, as the address of (&) operator give the address of the variable. so, scanf with variable name and address operator meane to write the value at this location.
You can also check the address of any variable with the address of(&) operator and %p format specifier in the hexadecimal format.
这是因为你正在存储一些东西。想想一个函数必须如何工作。对于 printf,您可以将其视为一个 void 函数,它只输出结果,然后就完成了。使用 scanf,您希望保留一些数据,因此您需要一个指针(又名地址),即使您离开该函数,您输入的数据也将存储在其中。如果您采用数据类型的参数,例如“int”,那么一旦您退出 scanf 函数,数据就会丢失,换句话说,在父函数的下一行代码中,您 scanfed 的数据将是走了。
It's because you are storing something. Just think about how a function must work. With printf, you can think of that as a void function that just outputs the result and then it is done. With scanf you are wanting to RETAIN some data, so you need a pointer aka address where the data you input will be stored even after you leave the function. If you took an argument of data type, say, "int", the data would be lost as soon as you exit the scanf function, in other words, in the next line of code in the parent function, that data you scanfed would be gone.