简体   繁体   English

为什么在此printf语句中需要连字符?

[英]Why do I need an ampersand for this printf statement?

Why do I need an ampersand before the array in the printf statement here, but any other time ampersand is only used in scanf? 为什么我在这里的printf语句中的数组之前需要一个&符,而其他任何时间&符仅在scanf中使用?

Completely new so if you could explain as simply as possible that would help. 全新的内容,因此,如果您能尽可能简单地解释,将会有所帮助。


int main(void) {

  char word[1];
  scanf("%s", &word[0]);
  printf("%s", word[0]);
  return 0;
}

You do need one. 您确实需要一个。 It's incorrect without one and leads to undefined behavior, which may cause anything to happen (even the program appearing to work correctly). 如果没有一个,它是不正确的,并且会导致未定义的行为,这可能会导致任何事情发生(甚至该程序似乎正常工作)。 Also, word[1] can only hold the null terminator of an empty string, any more than that and it will cause the buffer to overflow, also causing undefined behavior. 另外, word[1]只能容纳空字符串的null终止符,如果超过该数目,它将导致缓冲区溢出,还导致未定义的行为。 It should be: 它应该是:

int main(void) {
    char word[10]; // or any value that is big enough for the input that your anticipating
    scanf("%9s", &word[0]);
    printf("%s", &word[0]);
    return 0;
}

And of course you can replace &word[0] with word . 当然,您也可以替换&word[0]word

Also note that I put %9s instead of just %s for the scanf call, which means that it will get at most 9 characters, which with the added null terminator fits into the word[10] that we have as an example. 还要注意,我将%9s而不是%s用于scanf调用,这意味着它将最多获得9字符,加上添加的空终止符可适合我们作为示例的word[10] This way you don't get undefined behavior if the user enters something that's too big and instead it would just truncate that input. 这样,如果用户输入的内容太大,您就不会得到不确定的行为,而只会截断该输入。

word[0] is the first character. word[0]是第一个字符。 the & sign is used to get a pointer to such character. &符号用于获取指向此类字符的指针

scanf("%s", &word[0]);
printf("%s", word[0]);

In C, a string (addressed by %s in above statements) is defined as an array. 在C语言中,字符串(在上面的语句中以%s寻址)定义为数组。 Your declaration of "word": 您对“单词”的声明:

char word[1]; 字符字[1];

declares an array, so that array can be a string (I wrote can , because it's a very short string...). 声明一个数组,以便该数组可以是一个字符串(我写了can ,因为它是一个很短的字符串...)。 If you use the identifier alone, "word", the compiler uses a pointer to the first element of the array. 如果仅使用标识符“ word”,则编译器将使用指向数组第一个元素的指针。 Hence: 因此:

scanf("%s", word);
printf("%s", word);

are both correct. 都是正确的。 But if you use an index into the array, like "word[0]", then you are no more using the array per se, but instead an element of it . 但是,如果您使用数组的索引(例如“ word [0]”),那么您将不再使用数组本身,而是使用数组的元素 This notation does not generate a pointer, but your above statements do need a pointer. 该表示法不会生成指针,但是您的上述语句确实需要一个指针。 So you can use an "&" to get a pointer. 因此,您可以使用“&”获取指针。

Conclusion: if you have a string, use its simple name without indexing inside the array. 结论:如果您有字符串,请使用其简单名称而不在数组内建立索引。 Using the ampersand makes it clear that we pass a reference, but "&word[0]" is ugly and, moreover, everybody should know that scanf() writes into its arguments. 使用&号可以清楚地表明我们传递了一个引用,但是“&word [0]”很丑陋,而且,每个人都应该知道scanf() 写入其参数。

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

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