4
They do two different things.
他们做了两件不同的事情。
char str1[] = "Hello";
is equivalent to
相当于
char str1[6] = "Hello";
It makes str1
an array of 6 char
s, initialized to "Hello"
plus a '\0'
terminator.
它使str1成为一个包含6个字符的数组,初始化为“Hello”加上一个'\ 0'终止符。
This:
char *str2 = "World";
makes str2
a pointer to char
, pointing to the first element of a statically allocated read-only array of 6 char
s, which is initialized to "World"
plus a '\0'
terminator.
使str2成为指向char的指针,指向静态分配的6个字符的只读数组的第一个元素,该数组初始化为“World”加上一个'\ 0'终止符。
I mentioned that the array is read-only -- but for historical reasons it's not actually const
; attempting to change it has undefined behavior. You should define it as const
so the compiler will warn you if you incorrectly attempt to modify it:
我提到数组是只读的 - 但由于历史原因,它实际上不是常量;试图改变它有未定义的行为。您应该将其定义为const,以便编译器在您错误地尝试修改它时会发出警告:
const char *str2 = "World";
str1
and str2
can be used interchangeably in some, but not all, contexts. For example, either puts(str1)
or puts(str2)
will print the corresponding string to standard output (followed by a newline). This is because an array name, in most contexts, "decays" to a pointer to the array's first element.
str1和str2可以在一些但不是所有的上下文中互换使用。例如,put(str1)或puts(str2)将相应的字符串打印到标准输出(后跟换行符)。这是因为在大多数情况下,数组名称“衰减”到指向数组第一个元素的指针。
Since str1
is an array, you can apply sizeof
to it to determine how big the array is; sizeof str2
gives you the size of a char*
pointer, which probably won't be useful.
由于str1是一个数组,您可以对其应用sizeof来确定数组的大小; sizeof str2为您提供char *指针的大小,这可能没用。
With the first declaration, you can modify the characters of the array, but you can't change its size, and you can't make str1
refer to a different array.
使用第一个声明,您可以修改数组的字符,但不能更改其大小,并且不能使str1引用不同的数组。
With the second declaration, you can't modify the characters of the array, but you can change the pointer value itself so it points to some other array, or to nothing if you do str2 = NULL;
.
使用第二个声明,您不能修改数组的字符,但您可以更改指针值本身,使其指向其他数组,如果您执行str2 = NULL,则不会改为任何内容。
If all you want to do with str1
or str2
is pass it to something that expects a char*
that points to a string, you can use either form. The pointer form makes it a bit clearer that you're not going to be modifying the string. You can even write:
如果你想用str1或str2做的只是将它传递给需要指向字符串的char *的东西,你可以使用任何一种形式。指针形式使您更加清楚,您不会修改字符串。你甚至可以写:
const char *const str2 = "World";
if you want to prevent modifying either the string or the pointer.
如果你想阻止修改字符串或指针。
If you want to do something else, one form or the other may have some advantages, but I can't comment further without knowing what you're using it for.
如果你想做别的事情,一种形式或另一种形式可能有一些优势,但我不能在不知道你用它的情况下进一步评论。
There shouldn't be any significant difference in efficiency. Concentrate first on what how you want your code to behave. Adding const
to anything you don't intend to modify helps document your intent and may help the compiler generate better code (because it doesn't need to assume that something may have been modified).
效率应该没有任何显着差异。首先关注您希望代码的行为方式。将const添加到您不打算修改的任何内容有助于记录您的意图,并可能有助于编译器生成更好的代码(因为它不需要假设某些内容可能已被修改)。
Recommended reading: the comp.lang.c FAQ, particularly section 6 which covers arrays and pointers.
推荐阅读:comp.lang.c FAQ,特别是第6节,其中包括数组和指针。