C:为什么没有和放大器;在scanf()的函数字符串? [英] C: why no & for strings in scanf() function?

查看:129
本文介绍了C:为什么没有和放大器;在scanf()的函数字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1931850/why-doesnt-scanf-need-an-ampersand-for-strings-and-also-works-fine-in-printf-i\">Why不scanf函数需要字符串的符号和printf中也能正常工作(C语言)?

在c当我们使用 scanf()的函数来获取用户输入,我们总是使用&安培; 变量之前签署。
例如:

In C when we use the scanf() function to get user input, we always use the & sign before the variable. For example:

  scanf("%d", &number);
  scanf("%c", &letter);

这确保了我们的投入是存储在正确的地址。但是,在一个字符串的情况下,我们不使用&放大器;

This ensures our input is stored in proper address. But in case of a string we do not use &.

这是为什么?

推荐答案

在C A串是一个字符缓冲区的地址。结果
你想 scanf函数填写缓冲区,这是由变量指向的内存。

A "string" in C is the address of a character buffer.
You want scanf to fill the memory in the buffer, which is pointed to by the variable.

相反, INT 是一个内存块,而不是一个地址。为了让 scanf函数来填补的记忆,你需要传递它的地址。

In contrast, an int is a block of memory, not an address. In order for scanf to fill that memory, you need to pass its address.

这篇关于C:为什么没有和放大器;在scanf()的函数字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆