为什么scanf必须取操作符的地址

发布于 2024-09-26 09:20:09 字数 83 浏览 6 评论 0原文

正如标题所说,我总是想知道为什么 scanf 必须采用 address of 运算符(&)。

As the title says, I always wonder why scanf must take the address of operator (&).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

萤火眠眠 2024-10-03 09:20:09

因为 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).

夏尔 2024-10-03 09:20:09

scanf 不采用“运算符 (&) 的地址”。它需要一个指针。大多数情况下,指向输出变量的指针是通过在 scanf 调用中使用地址运算符来获取的,例如

int i;
scanf("%i", &i);
printf("number is: %d\n", i);

但这不是唯一的方法。以下内容同样有效:

int *iPtr = malloc(sizeof(int));
scanf("%i", iPtr);
printf("number is: %d\n", *iPtr);

同样,我们可以使用以下代码执行相同的操作:

int i;
int *iPtr = &i;
scanf("%i", iPtr);
printf("number is: %d\n", i);

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.

int i;
scanf("%i", &i);
printf("number is: %d\n", i);

But that is not the only way to do it. The following is just as valid:

int *iPtr = malloc(sizeof(int));
scanf("%i", iPtr);
printf("number is: %d\n", *iPtr);

Likewise we can do the same thing with the following code:

int i;
int *iPtr = &i;
scanf("%i", iPtr);
printf("number is: %d\n", i);
断念 2024-10-03 09:20:09

因为它需要地址来放置它读取的值。如果将变量声明为指针,则 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 &.

季末如歌 2024-10-03 09:20:09

其他人都已经很好地描述了 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.

似梦非梦 2024-10-03 09:20:09

当您使用标准输入设备(通常是键盘)输入内容时,传入的数据必须存储某处。您必须指向内存中的某个位置,以便可以将数据存储在那里。要指向内存位置,您需要该位置的地址。因此,您必须使用 & 运算符和 scanf() 来传递变量的地址。

When you input something with standard input device (usually keyboard), the data that comes through must be stored somewhere. You must point somewhere in the memory so that data can be stored there. To point a memory location, you need the address of that location. So you must pass your variable's address by using & operator with scanf().

抚笙 2024-10-03 09:20:09

因为该值将存储在内存地址中(在哪里?)。所以 scanf() 处理 (&) 运算符。

as the value is going to be stored,(where?), in the memory address. so scanf() deals with (&) operator.

绝情姑娘 2024-10-03 09:20:09

它告诉在哪里写入输入值,因为 (&) 运算符的地址给出了变量的地址。因此,带有变量名和地址运算符的 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.

始终不够 2024-10-03 09:20:09

这是因为你正在存储一些东西。想想一个函数必须如何工作。对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文