C++ char* 和 char[] 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7564033/
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
Difference between char* and char[]
提问by Nemo
I know this is a very basic question. I am confused as to why and how are the following different.
我知道这是一个非常基本的问题。我很困惑为什么以及以下内容有何不同。
char str[] = "Test";
char *str = "Test";
采纳答案by K-ballo
char str[] = "Test";
Is an array of chars
, initialized with the contents from "Test", while
是一个数组chars
,用“Test”中的内容初始化,而
char *str = "Test";
is a pointer to the literal (const) string "Test".
是指向文字(常量)字符串“Test”的指针。
The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test"
, while the pointer simply refers to the contents of the string (which in this case is immutable).
它们之间的主要区别在于第一个是数组,另一个是指针。数组拥有它的内容,它恰好是 的副本"Test"
,而指针只是指向字符串的内容(在这种情况下是不可变的)。
回答by Jon Reid
A pointer can be re-pointed to something else:
一个指针可以重新指向别的东西:
char foo[] = "foo";
char bar[] = "bar";
char *str = foo; // str points to 'f'
str = bar; // Now str points to 'b'
++str; // Now str points to 'a'
The last example of incrementing the pointer shows that you can easily iterate over the contents of a string, one element at a time.
增加指针的最后一个示例表明您可以轻松地迭代字符串的内容,一次一个元素。
回答by Luka Rahne
One is pointer and one is array. They are different type of data.
一种是指针,一种是数组。它们是不同类型的数据。
int main ()
{
char str1[] = "Test";
char *str2 = "Test";
cout << "sizeof array " << sizeof(str1) << endl;
cout << "sizeof pointer " << sizeof(str2) << endl;
}
output
输出
sizeof array 5
sizeof pointer 4
回答by rnrneverdies
The diference is the STACK memory used.
不同之处在于使用的堆栈内存。
For example when programming for microcontrollers where very little memory for the stack is allocated, makes a big difference.
例如,当为微控制器编程时,为堆栈分配的内存很少,这会产生很大的不同。
char a[] = "string"; // the compiler puts {'s','t','r','i','n','g', 0} onto STACK
char *a = "string"; // the compiler puts just the pointer onto STACK
// and {'s','t','r','i','n','g',0} in static memory area.
回答by Peter Olson
The first
首先
char str[] = "Test";
is an array of five characters, initialized with the value "Test"
plus the null terminator '\0'
.
是一个包含五个字符的数组,用值"Test"
加上空终止符初始化'\0'
。
The second
第二
char *str = "Test";
is a pointer to the memory location of the literal string "Test"
.
是指向文字字符串内存位置的指针"Test"
。
回答by Matthieu Brucher
Starting from C++11, the second expression is now invalid and must be written:
从 C++11 开始,第二个表达式现在无效,必须写成:
const char *str = "Test";
The relevant section of the standard is Appendix C section 1.1:
该标准的相关章节是附录C第1.1节:
Change: String literals made const
The type of a string literal is changed from “array of char” to “array of const char.” The type of a char16_t string literal is changed from “array of some-integer-type” to “array of const char16_t.” The type of a char32_t string literal is changed from “array of some-integer-type” to “array of const char32_t.” The type of a wide string literal is changed from “array of wchar_t” to “array of const wchar_t.”
Rationale: This avoids calling an inappropriate overloaded function, which might expect to be able to modify its argument.
Effect on original feature: Change to semantics of well-defined feature.
更改:字符串文字常量
字符串文字的类型从“字符数组”更改为“常量字符数组”。char16_t 字符串文字的类型从“某种整数类型的数组”更改为“const char16_t 的数组”。char32_t 字符串文字的类型从“某种整数类型的数组”更改为“const char32_t 的数组”。宽字符串文字的类型从“wchar_t 数组”更改为“const wchar_t 数组”。
基本原理:这避免了调用不合适的重载函数,该函数可能期望能够修改其参数。
对原始特征的影响:更改为明确定义的特征的语义。
回答by Tim
"Test"
is an array of five characters (4 letters, plus the null terminator.
"Test"
是一个包含五个字符的数组(4 个字母,加上空终止符。
char str1[] = "Test";
creates that array of 5 characters, and names it str1
. You can modify the contents of that array as much as you like, e.g. str1[0] = 'B';
char str1[] = "Test";
创建 5 个字符的数组,并将其命名为str1
. 您可以根据需要修改该数组的内容,例如str1[0] = 'B';
char *str2 = "Test";
creates that array of 5 characters, doesn't name it, and also creates a pointer named str2
. It sets str2
to point at that array of 5 characters. You can follow the pointer to modify the array as much as you like, e.g. str2[0] = 'B';
or *str2 = 'B';
. You can even reassign that pointer to point someplace else, e.g. str2 = "other";
.
char *str2 = "Test";
创建 5 个字符的数组,不命名它,并创建一个名为str2
. 它设置str2
为指向该 5 个字符的数组。您可以按照指针随意修改数组,例如str2[0] = 'B';
或*str2 = 'B';
。您甚至可以重新分配该指针以指向其他地方,例如str2 = "other";
.
An array isthe text in quotes. The pointer merely points at it. You can do a lot of similar things with each, but they are different:
数组是引号中的文本。指针只是指向它。你可以用它们做很多类似的事情,但它们是不同的:
char str_arr[] = "Test";
char *strp = "Test";
// modify
str_arr[0] = 'B'; // ok, str_arr is now "Best"
strp[0] = 'W'; // ok, strp now points at "West"
*strp = 'L'; // ok, strp now points at "Lest"
// point to another string
char another[] = "another string";
str_arr = another; // compilation error. you cannot reassign an array
strp = another; // ok, strp now points at "another string"
// size
std::cout << sizeof(str_arr) << '\n'; // prints 5, because str_arr is five bytes
std::cout << sizeof(strp) << '\n'; // prints 4, because strp is a pointer
for that last part, note that sizeof(strp) is going to vary based on architecture. On a 32-bit machine, it will be 4 bytes, on a 64-bit machine it will be 8 bytes.
对于最后一部分,请注意 sizeof(strp) 将因架构而异。在 32 位机器上,它将是 4 个字节,在 64 位机器上它将是 8 个字节。