C语言 使用 scanf 读取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5406935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:07:57  来源:igfitidea点击:

Reading a string with scanf

cscanf

提问by abeln

I'm a little bit confused about something. I was under the impression that the correct way of reading a C string with scanf()went along the lines of

我对某事有点困惑。我的印象是,阅读 C 字符串的正确方法scanf()

(never mind the possible buffer overflow, it's just a simple example)

(别介意可能的缓冲区溢出,这只是一个简单的例子)

char string[256];
scanf( "%s" , string );

However, the following seems to work too,

但是,以下似乎也有效,

scanf( "%s" , &string );

Is this just my compiler (gcc), pure luck, or something else?

这只是我的编译器(gcc),纯粹的运气,还是其他什么?

采纳答案by Gareth McCaughan

An array "decays" into a pointer to its first element, so scanf("%s", string)is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string)passes a pointer-to-char[256], but it points to the same place.

数组“衰减”为指向其第一个元素的指针,因此scanf("%s", string)等效于scanf("%s", &string[0])。另一方面,scanf("%s", &string)传递一个指向-的指针char[256],但它指向同一个地方。

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in stringor &string[0], but when you've passed in &stringyou're depending on something that the language standard doesn't guarantee, namely that the pointers &stringand &string[0]-- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

然后scanf,在处理其参数列表的尾部时,将尝试拉出一个char *. 当你传入stringor 时&string[0],这是正确的事情,但是当你传入时,&string你依赖于语言标准不能保证的东西,即指针&string&string[0]-- 指向不同类型和大小的对象的指针从同一个地方开始——以同样的方式表示。

I don't believe I've ever encountered a system on which that doesn't work, and in practice you're probably safe. None the less, it's wrong, and it could fail on some platforms. (Hypothetical example: a "debugging" implementation that includes type information with every pointer. I thinkthe C implementation on the Symbolics "Lisp Machines" did something like this.)

我不相信我曾经遇到过无法运行的系统,实际上您可能是安全的。尽管如此,这是错误的,并且在某些平台上可能会失败。(假设示例:包含每个指针的类型信息的“调试”实现。我认为Symbolics“Lisp Machines”上的 C 实现做了类似的事情。)

回答by angelita

I think that this below is accurate and it may help. Feel free to correct it if you find any errors. I'm new at C.

我认为以下内容是准确的,可能会有所帮助。如果您发现任何错误,请随时更正。我是 C 的新手。

char str[]  
  1. array of values of type char, with its own address in memory
  2. array of values of type char, with its own address in memory as many consecutive addresses as elements in the array
  3. including termination null character '\0'&str, &str[0]and str, all three represent the same location in memory which is address of the first element of the array str

    char *strPtr = &str[0]; //declaration and initialization

  1. char 类型的值数组,在内存中具有自己的地址
  2. char 类型值的数组,它在内存中的地址与数组中元素的连续地址一样多
  3. 包括终止空字符'\0'&str&str[0]并且str,所有三个代表存储器中的相同位置这是所述阵列的所述第一元件的地址str

    字符 *strPtr = &str[0]; //声明和初始化

alternatively, you can split this in two:

或者,您可以将其分为两部分:

char *strPtr; strPtr = &str[0];
  1. strPtris a pointer to a char
  2. strPtrpoints at array str
  3. strPtris a variable with its own address in memory
  4. strPtris a variable that stores value of address &str[0]
  5. strPtrown address in memory is different from the memory address that it stores (address of array in memory a.k.a &str[0])
  6. &strPtrrepresents the address of strPtr itself
  1. strPtr是一个指向a的指针 char
  2. strPtr指向数组 str
  3. strPtr是一个变量,在内存中有自己的地址
  4. strPtr是一个存储地址值的变量 &str[0]
  5. strPtr自己在内存中的地址与它存储的内存地址不同(内存中数组的地址又名 &str[0])
  6. &strPtr代表strPtr本身的地址

I think that you could declare a pointer to a pointer as:

我认为您可以将指向指针的指针声明为:

char **vPtr = &strPtr;  

declares and initializes with address of strPtr pointer

用 strPtr 指针的地址声明和初始化

Alternatively you could split in two:

或者,您可以分为两部分:

char **vPtr;
*vPtr = &strPtr
  1. *vPtrpoints at strPtr pointer
  2. *vPtris a variable with its own address in memory
  3. *vPtris a variable that stores value of address &strPtr
  4. final comment: you can not do str++, straddress is a const, but you can do strPtr++
  1. *vPtr指向 strPtr 指针
  2. *vPtr是一个变量,在内存中有自己的地址
  3. *vPtr是一个存储地址值的变量 &strPtr
  4. 最后评论:你不能做str++str地址是一个const,但你可以做strPtr++