简体   繁体   English

char* 和 char[] 之间的区别

[英]Difference between char* and char[]

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";
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"的副本,而指针只是指向字符串的内容(在这种情况下是不可变的)。

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.

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.增加指针的最后一个示例表明您可以轻松地迭代字符串的内容,一次一个元素。

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

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"的内存位置的指针。

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.” char16_t 字符串文字的类型从“某种整数类型的数组”更改为“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.” char32_t 字符串文字的类型从“某种整数类型的数组”更改为“const char32_t 的数组”。 The type of a wide string literal is changed from “array of wchar_t” to “array of const wchar_t.”宽字符串文字的类型从“wchar_t 数组”更改为“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.对原始特征的影响:更改为明确定义的特征的语义。

"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 .创建 5 个字符的数组,并将其命名为str1 You can modify the contents of that array as much as you like, eg str1[0] = 'B';您可以随意修改该数组的内容,例如str1[0] = 'B';

char *str2 = "Test"; creates that array of 5 characters, doesn't name it, and also creates a pointer named str2 .创建 5 个字符的数组,不命名它,并创建一个名为str2的指针。 It sets str2 to point at that array of 5 characters.它将str2设置为指向该 5 个字符的数组。 You can follow the pointer to modify the array as much as you like, eg str2[0] = 'B';您可以按照指针随意修改数组,例如str2[0] = 'B'; or *str2 = 'B';*str2 = 'B'; . . You can even reassign that pointer to point someplace else, eg str2 = "other";您甚至可以重新分配该指针指向其他地方,例如str2 = "other"; . .

An array is the 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.对于最后一部分,请注意 sizeof(strp) 将因架构而异。 On a 32-bit machine, it will be 4 bytes, on a 64-bit machine it will be 8 bytes.在 32 位机器上,它将是 4 个字节,在 64 位机器上它将是 8 个字节。

Let's take a look at the following ways to declare a string:我们来看看以下几种声明字符串的方式:

char name0 = 'abcd';           // cannot be anything longer than 4 letters (larger causes error)
cout << sizeof(name0) << endl; // using 1 byte to store

char name1[]="abcdefghijklmnopqrstuvwxyz"; // can represent very long strings
cout << sizeof(name1) << endl;             // use large stack memory

char* name2 = "abcdefghijklmnopqrstuvwxyz"; // can represent very long strings
cout << sizeof(name2) << endl;              // but use only 8 bytes

We could see that declaring string using char* variable_name seems the best way!我们可以看到使用 char* variable_name 声明字符串似乎是最好的方法! It does the job with minimum stack memory required.它以最少的堆栈内存来完成这项工作。

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

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