简体   繁体   English

关于 strcpy 的说明

[英]Clarification on strcpy

I am currently learning C via edube.org and came across something that didn't quite agree with my observations when I run the code in Code blocks.我目前正在通过 edube.org 学习 C,当我在代码块中运行代码时,遇到了一些与我的观察不太一致的东西。

For example: edube.org says that The invocation:例如: edube.org 说调用:

strcpy(string, "Alice has a cat"); strcpy(string, "爱丽丝有一只猫");

fills the array string with the string Alice.用字符串Alice填充数组字符串

I initially thought (just going by the above statement in edube.org) that this is probably because the space after "Alice" may have been considered a null character '\0' marking the end of the character.我最初认为(仅通过 edube.org 中的上述声明)这可能是因为“Alice”之后的空格可能被认为是 null 字符 '\0' 标记字符的结尾。 But this does not seem to be the case.但情况似乎并非如此。 I am just looking for clarification to convince myself that edube.org is indeed incorrect.我只是在寻求澄清以说服自己 edube.org 确实不正确。

Either you read wrong or the site is wrong.要么你读错了,要么网站错了。

strcpy(string, "Alice has a cat");

This fills the character array with the string "Alice has a cat" .这将使用字符串"Alice has a cat"填充字符数组。

The null character (ASCII code 0) is a distinct character that cannot be typed from the keyboard directly. null 字符(ASCII 代码 0)是一个独特的字符,不能直接从键盘输入。 In C, it is represented by \0 and is appended by default at the end of all string literals.在 C 中,它由\0表示,并且默认附加在所有字符串文字的末尾。 It should not be confused with the space character (ASCII code 32), which is just a regular character.它不应与空格字符(ASCII 代码 32)混淆,后者只是一个常规字符。

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

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