这些声明之间的技术差异是什么?
char amessage[] = "now is the time"; /* an array */
char *pmessage = "now is the time"; /* a pointer */
char amessage[] = "now is the time"; /* an array */
char *pmessage = "now is the time"; /* a pointer */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
amessage
的类型为char[16]
。它是一个数组。数组的元素包含字符串文字中的十六个字符。pmessage
的类型为char*
。它是一个指针。它指向一个(不可修改的)数组,其中包含字符串文字中的十六个字符。 (您应该避免使用char*
来指向字符串文字;这样做是邪恶的。在引用字符串文字时,您应该尽可能使用const char*
。 )amessage
is of typechar[16]
. It is an array. The elements of the array contain the sixteen characters from the string literal.pmessage
is of typechar*
. It is a pointer. It points to a (non-modifiable) array containing the sixteen characters from the string literal. (You should avoid using achar*
to point to a string literal; doing so is evil. You should always use aconst char*
wherever possible when referring to string literals.)除了 James McNellis 的回答之外,
amessage
是一回事 - 堆栈上的 16 个字符的数组恰好包含字符串“now is the time”(因为当创建一条消息
)。您可以更改它包含的内容。另一方面,pmessage 是两个东西——一个字符串文字(存储在内存的不可写部分)和一个指针。您无法更改字符串文字的内容,但可以更改指针指向的内容,并使其指向不同的字符串。您可以使用字符串文字作为其他指针的目标。
(从某种意义上说,这并不完全正确 -
amessage
还涉及字符串文字,因为它是创建内容时复制内容的地方。但是您不能做任何其他事情 /em> 之后带有该字符串文字。)In addition to James McNellis's answer,
amessage
is one thing -- an array of 16 characters on the stack that happens to contain the string "now is the time" (because they are copied there whenamessage
is created). You can change what it contains.On the other hand,
pmessage
is two things -- a string literal (which is stored in a non-writable portion of memory), and a pointer. You can't change the contents of the string literal, but you can change what the pointer points to, and make it point at a different string. And you can use the string literal as a target of other pointers.(In some sense, this is not entirely true --
amessage
also involves a string literal, as it's where the contents are copied from when it's created. But you can't do anything else with that string literal afterwards.)如果您使用的是 GCC,请打开 -Wwrite-strings。固定字符串的类型为 const char[length_of_string],转换为 char * 将引发警告 [needs to be const]。
第一个赋值是字符数组赋值,而第二个赋值是基于指针的赋值(结果字符串作为固定字符串保存)
第一个赋值按原样可接受,而第二个赋值需要 const 限定符。
在第一个分配中,更改点是可以接受的(例如 amessage[3] = 'q')。在第二个作业中,改变一个点是不可接受的(因为字符串是 const )——你应该得到一个总线错误
if you are using GCC, turn on -Wwrite-strings. fixed strings are of type const char[length_of_string], and the conversion to a char * will elicit a warning [needs to be const].
The first assignment is a character array assignment, whereas the second assignment is a pointer-based assignment (and the resulting string is held as a fixed string)
The first assignment is acceptable as-is, whereas the second one requires a const qualifier.
In the first assignment, changing a point is acceptable (e.g. amessage[3] = 'q'). in the second assignment, changing a point is unacceptable (since the string is const ) -- you should get a bus error
在 C 语言中,“=”左边的东西称为“左值”。该类型仅由“左值”定义。所以'message'是一个字符数组。 'pmessage' 是一个指向字符的指针。
在 C 中,更多关于数组类型和数组类型之间的等效关系。 'char *' 访问,以及什么不是。
正如所声明的“pmessage”是一个可修改的“左值”-“amessage”是不可修改的。因此,建议将“pmessage”声明为 const,因为实际上它也不应该被修改。
例如,
就内存访问而言,“amessage”导致直接访问,而“pmessage”则需要取消引用。
注意:C 编译器仅允许在编译时通过字符串文字初始化“char *”。
例如,如果您乘坐
4 号线,则毫无意义 &将是非法的。
强烈推荐 Peter Van Linden 的《Deep C Secrets》,其中有一整章是关于 C 数组和 C 语言的。指针。
In C the thing to the left of '=' is termed an 'lvalue'. The type is defined solely by the 'lvalue'. So 'amessage' is a char array. 'pmessage' is a pointer to a char.
In C more goes on in relation to what is equivalent between array type & 'char *' accesses, and also what is not.
As declared 'pmessage' is a modifiable 'lvalue' - 'amessage' is not modifiable. Hence the advise to declare 'pmessage' as const as really it should not be modifiable also.
E.g.
In terms of memory access 'amessage' results in a direct access, whereas 'pmessage' requires a dereference.
Note: the C compiler only permits initialisation of 'char *' at compile time via a string literal.
E.g. If you had
Line 4 makes no sense & would be illegal.
Highly recommend Peter Van Linden's 'Deep C Secrets' which he has a whole chapter about C arrays & pointers.