如何用C [duplicate]声明字符串

时间:2021-08-11 21:29:17

Possible Duplicate:
Memory Allocation char* and char[]

可能的副本:内存分配char*和char[]

Can anyone explain me what is a difference between these lines of code

谁能给我解释一下这两行代码的区别吗

char *p = "String";
char p2[] = "String";
char p3[7] = "String";

In what case should I use each of the above ?

在什么情况下我应该使用上面的每一个?

4 个解决方案

#1


35  

This link should satisfy your curiosity.

这个链接应该能满足你的好奇心。

Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.

基本上(忘记了第三个不好的例子),1和2之间的区别在于1为指向数组的指针分配了空间。

But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.

但是在代码中,你可以把它们作为指针来操作——只是,你不能重新分配第二个。

#2


30  

Strings in C are represented as arrays of characters.

C中的字符串表示为字符数组。

char *p = "String";

You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.

您正在声明一个指针,该指针指向在您的程序中存储的某个字符串(根据C编程语言2 ed修改此字符串是未定义的行为)。

char p2[] = "String";

You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.

您正在声明一个用字符串“string”初始化的char数组,留给编译器计算数组大小的任务。

char p3[5] = "String";

You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.

您正在声明一个大小为5的数组并使用“String”初始化它。这是一个错误,因为“字符串”不适合5个元素。

char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).

char p3[7]=“字符串”;是正确的声明('\0'是c字符串中的终止字符)。

http://c-faq.com/~scs/cclass/notes/sx8.html

http://c-faq.com/ scs / cclass / notes / sx8.html

#3


20  

You shouldn't use the third one because its wrong. "String" takes 7 bytes, not 5.

你不应该用第三个,因为它是错的。“String”需要7个字节,而不是5个字节。

The first one is a pointer (can be reassigned to a different address), the other two are declared as arrays, and cannot be reassigned to different memory locations (but their content may change, use const to avoid that).

第一个是指针(可以重新分配到不同的地址),另外两个被声明为数组,不能重新分配到不同的内存位置(但是它们的内容可能会改变,使用const来避免)。

#4


7  

char *p = "String";   means pointer to a string type variable.

char p3[5] = "String"; means you are pre-defining the size of the array to consist of no more than 5 elements. Note that,for strings the null "\0" is also considered as an element.So,this statement would give an error since the number of elements is 7 so it should be:

char p3[5]=“字符串”;意味着您正在预先定义由不超过5个元素组成的数组的大小。注意,对于字符串,null“\0”也被视为元素。因此,这个语句会产生一个错误,因为元素的数量是7,所以它应该是:

char p3[7]= "String";

#1


35  

This link should satisfy your curiosity.

这个链接应该能满足你的好奇心。

Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.

基本上(忘记了第三个不好的例子),1和2之间的区别在于1为指向数组的指针分配了空间。

But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.

但是在代码中,你可以把它们作为指针来操作——只是,你不能重新分配第二个。

#2


30  

Strings in C are represented as arrays of characters.

C中的字符串表示为字符数组。

char *p = "String";

You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.

您正在声明一个指针,该指针指向在您的程序中存储的某个字符串(根据C编程语言2 ed修改此字符串是未定义的行为)。

char p2[] = "String";

You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.

您正在声明一个用字符串“string”初始化的char数组,留给编译器计算数组大小的任务。

char p3[5] = "String";

You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.

您正在声明一个大小为5的数组并使用“String”初始化它。这是一个错误,因为“字符串”不适合5个元素。

char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).

char p3[7]=“字符串”;是正确的声明('\0'是c字符串中的终止字符)。

http://c-faq.com/~scs/cclass/notes/sx8.html

http://c-faq.com/ scs / cclass / notes / sx8.html

#3


20  

You shouldn't use the third one because its wrong. "String" takes 7 bytes, not 5.

你不应该用第三个,因为它是错的。“String”需要7个字节,而不是5个字节。

The first one is a pointer (can be reassigned to a different address), the other two are declared as arrays, and cannot be reassigned to different memory locations (but their content may change, use const to avoid that).

第一个是指针(可以重新分配到不同的地址),另外两个被声明为数组,不能重新分配到不同的内存位置(但是它们的内容可能会改变,使用const来避免)。

#4


7  

char *p = "String";   means pointer to a string type variable.

char p3[5] = "String"; means you are pre-defining the size of the array to consist of no more than 5 elements. Note that,for strings the null "\0" is also considered as an element.So,this statement would give an error since the number of elements is 7 so it should be:

char p3[5]=“字符串”;意味着您正在预先定义由不超过5个元素组成的数组的大小。注意,对于字符串,null“\0”也被视为元素。因此,这个语句会产生一个错误,因为元素的数量是7,所以它应该是:

char p3[7]= "String";