为什么 scanf() 需要 &运算符(地址)在某些情况下,而不是其他情况下?

发布于 2024-09-13 16:44:33 字数 344 浏览 13 评论 0原文

为什么我们需要在 scanf() 中放置 & 运算符来将值存储在整数数组中,而不是将字符串存储在字符数组中?

int a[5];
for(i=0;i<5;i++)
scanf("%d",&a[i]);

但是

char s[5]; scanf("%s",s);

我们需要传入存储值的位置的地址,因为数组是指向第一个元素的指针。因此,在 int/float 数组的情况下,它基本上意味着 (a+i)

但字符串的情况又如何呢?

Why do we need to put a & operator in scanf() for storing values in an integer array but not while storing a string in a char array?

int a[5];
for(i=0;i<5;i++)
scanf("%d",&a[i]);

but

char s[5]; scanf("%s",s);

We need to pass in the address of the place we store the value, since array is a pointer to first element. So in the case with int/float arrays it basically means (a+i).

But whats the case with strings?

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

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

发布评论

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

评论(5

意中人 2024-09-20 16:44:33

scanf 接受一个指向您要放入值的内容的指针。在第一个实例中,您传递对整数数组中位置 i 处的特定 int 的引用。在第二个实例中,您将整个数组传递给 scanf。在 C 语言中,数组和指针是同义词,可以(某种程度上)互换使用。变量 s 实际上是一个指向内存的指针,该内存有 5 个字符的连续空间。

scanf accepts a pointer to whatever you are putting the value in. In the first instance, you are passing a reference to the specific int at position i in your integer array. In the second instance you are passing the entire array in to scanf. In C, arrays and pointers are synonymous and can be used interchangeably (sort of). The variable s is actually a pointer to memory that has contiguous space for 5 characters.

不爱素颜 2024-09-20 16:44:33

当您在表达式中使用数组名称时(除了作为 sizeof 的操作数或地址运算符 &),它将计算为数组的地址该数组中的第一项——即指针值。这意味着不需要 & 来获取地址。

当您在表达式中使用 int(或短整型、长整型、字符型、浮点型、双精度型等)时(同样,作为 sizeof& 的操作数除外)它评估该对象的值。要获取地址(即指针值),您需要使用 & 来获取地址。

When you use the name of an array in an expression (except as the operand of sizeof or the address-of operator &), it will evaluate to the address of the first item in that array -- i.e., a pointer value. That means no & is needed to get the address.

When you use an int (or short, long, char, float, double, etc.) in an expression (again, except as the operand of sizeof or &) it evaluates to the value of that object. To get the address (i.e., a pointer value) you need to use the & to take the address.

半寸时光 2024-09-20 16:44:33
  • 用于通过 scanf() 接收值的所有变量都必须通过其地址进行传递。这意味着所有参数必须指向用作参数的变量。

    scanf("%d", &count);

  • 将字符串读入字符数组,不带任何索引的数组名就是数组第一个元素的地址。所以要读取将字符串转化为字符数组地址,我们使用

    scanf("%s",address);

  • 在这种情况下,address 已经是一个指针,不需要在前面加上 & 。

  • All the variables used to recieve values through scanf() must be passed by their addresses. This means that all arguments must be pointed to the variables used as arguments.

    scanf("%d", &count);

  • Stings are read into character arrays, and the array name without any index is the address of the first element of the array.So to read a string into a character array address, we use

    scanf("%s",address);

  • In this case address is already a pointer and need not to be preceded by the & operator.

毁我热情 2024-09-20 16:44:33

scanf("%d", a + i ) 也有效。

%d%s 只是告诉 scanf 期望什么,但在这两种情况下它都期望 C 中的地址

,数组和指针是相关的。

%s 只是告诉 scanf 期待一个以 \0 结尾的字符串,无论它是否适合字符数组scanf 不在乎。

scanf("%d", a + i ) works too.

%d and %s just tell scanf what to expect but in both cases it expects an address

in C, arrays and pointers are related.

%s just says to scanf to expect a string which is \0 terminated, whether it will fit into the character array or not scanf doesn't care.

月亮是我掰弯的 2024-09-20 16:44:33

因为字符数组已经是指针。

您可以将 C 数组视为指向堆栈分配的 RAM 量的指针。您甚至可以对它们使用指针操作而不是数组索引。 *aa[0] 都产生相同的结果(返回数组中的第一个字符)。

Because characters arrays are already pointers.

You can think of C arrays as pointers to a stack-allocated amount of RAM. You can even use pointer operations on them instead of array indexing. *a and a[0] both produce the same result (returning the first character in the array).

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