在 scanf() 中使用 & 符号

发布于 2024-08-25 00:28:42 字数 304 浏览 3 评论 0原文

当我编译 scanf("%s", &var); 时,gcc 发回警告:

warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’

但是当我编译 scanf("%s", var); 时,不应用任何警告。这两段代码都有效,而且我正在阅读的书特别提到要使用&符号,但即使在某些示例中也没有。

我的问题是,即使书中没有指定,我是否应该继续使用&符号?

When I compile scanf("%s", &var);, gcc sends back a warning:

warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’

however when I compile scanf("%s", var);, no warning is applied. Both pieces of code work and the book I am reading specifically says to use the ampersand, but even it doesn't in some of the examples.

My question is, should I continue to use the ampersand, even when the book doesn't specify?

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

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

发布评论

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

评论(4

眼泪都笑了 2024-09-01 00:28:42

从您发布的内容来看, var 是一个 char 数组。在这种情况下,您不需要 & 符号,只需名称 var 将根据需要计算为 (char *)。

详细信息:

scanf 需要一个指向将存储输入的变量的指针。对于字符串,您需要一个指向内存中字符数组的指针,该数组足够大以存储读入的任何字符串。当您声明诸如 char var[100] 之类的内容时,您会为100 个 char,其中 var[0] 引用第一个字符,var[99] 引用第 100 个字符。数组名称本身的计算结果与 &var[0] 完全相同,它是指向序列第一个字符的指针,这正是 scanf 所需要的。因此,您需要做的就是 scanf("%s", var);,但请注意 scanf 不会对输入字符串强制执行大小限制,因此如果用户输入 101 长度的字符串将会导致缓冲区溢出,这将导致错误,甚至更糟糕的安全问题。更好的选择通常是 fgets ,它确实允许输入字符串的大小限制。

From what you've posted var is a char array. In that case, you don't need the ampersand, just the name var will evaluate to a (char *) as needed.

Details:

scanf needs a pointer to the variable that will store input. In the case of a string, you need a pointer to an array of characters in memory big enough to store whatever string is read in. When you declare something like char var[100], you make space for 100 chars with var[0] referring to the first char and var[99] referring to the 100th char. The array name by itself evaluates to exactly the same thing as &var[0], which is a pointer to the first character of the sequence, exactly what is needed by scanf. So all you need to do is scanf("%s", var);, but be aware that scanf does not enforce size constraints on input strings, so if the user inputs a 101 length string your will have a buffer overrun, which will result in bugs or, even worse, security problems. The better choice is generally fgets which does allow size constraints for input strings.

一个人的旅程 2024-09-01 00:28:42

我邀请其他回答者在这里给出指导和参考的良好总结。我认为我不能在不犯一些错误的情况下做到这一点,而且我无意应对随之而来的书呆子愤怒。

因此,我将向您指出一个很好的资源来了解指针和引用这是你问题的核心。 http://www.cplusplus.com/doc/tutorial/pointers/

I invite any other answerers to give a good summary of pointers and references here. I don't think I can do that without making some mistakes here and there, and I have no intent to deal with the nerd rage that will follow.

So I'll just point you to a good resource to learn about pointers and references which lies at the heart of your problem. http://www.cplusplus.com/doc/tutorial/pointers/

桃扇骨 2024-09-01 00:28:42

scanf("%s", &var); 中删除 &,因此它是 scanf("%s", var);.

Remove the & from the scanf("%s", &var); so it is scanf("%s", var);.

数理化全能战士 2024-09-01 00:28:42

当你使用这样的数组时:

char name[20];

你必须考虑到一个由20个字符组成的内存区域与其相关联。要获取该内存区域开头的地址,您必须使用数组的名称,而要获取该数组的单个字符的地址,您必须使用如下语法: &name[i].< br>
您正在使用 scanf 读取字符串,该字符串是 C 中的字符数组。 scanf 需要与要读取的变量类型关联的内存区域的地址。在本例中,您正在读取一个数组,因此只需使用名称。
如果您想读取单个变量而不是数组,则必须使用&符号。例如:

char myChar;
scanf("%c", &myChar);

我希望这对您有所帮助。

When you use an array like this one:

char name[20];

you must consider that a memory area composed by 20 chars is associated with it. To obtain the address of the beginning of that memory area you must use the name of the array, while to obtain the address of a single char of that array, you have to use a sintax like this: &name[i].
You are using scanf to read a string, which is an array of char in C. scanf require the address of the memory area associated to the type of variable to read. In this case, you are reading an array so you have only to use the name.
If you want to read a single variable and not an array you have to use the ampersand. For example:

char myChar;
scanf("%c", &myChar);

I hope this can be helpful.

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