简体   繁体   English

在C中创建字符串的方法有什么区别?

[英]What is the difference between the ways to create a string in C?

What is the difference between these two forms of a string variable in C language? C语言中这两种形式的字符串变量有什么区别?

char *string1;
char string2[];

Is there any other way to do it? 还有其他办法吗?

Thank you very much. 非常感谢你。

char *string1 = "foo";

string1 is a pointer to a string literal (for the sake of argument, it points to a series of characters stored in a read-only data segment of the program). string1是指向字符串文字的指针(为了参数,它指向存储在程序的只读数据段中的一系列字符)。

char string2[] = "foo";

string2 is an array of 4 characters. string2是一个包含4个字符的数组。 It is initialised with the bytes 'f', 'o', 'o', ASCII_NUL. 它用字节'f','o','o',ASCII_NUL初始化。

Probably the most significant difference is that if you do string1[0] = 'b'; 可能最显着的区别是,如果你做string1[0] = 'b'; you get undefined behaviour, because you're trying to modify the stored representation a string literal. 你得到未定义的行为,因为你试图修改存储的表示形式的字符串文字。 If you do string2[0] = 'b'; 如果你做string2[0] = 'b'; then you modify your personal string to "boo", which is fine. 然后你将你的个人字符串修改为“boo”,这很好。

In general, a variable of type char* is a pointer to a char. 通常, char*类型的变量是指向char的指针。 It's often used to point to the first char in a NUL-terminated sequence of chars, in which case it points to a string. 它通常用于指向NUL终止的字符序列中的第一个字符,在这种情况下,它指向一个字符串。 A variable of type char[] is an array of chars. char[]类型的变量是一个chars数组。 If it has a NUL terminator, then it actually is a string. 如果它有一个NUL终止符,那么它实际上是一个字符串。

The issue is slightly confused by two facts: 两个事实略微混淆了这个问题:

1) In C, whenever an array variable name is used in a context that takes a pointer, it "means" a pointer to the first element of the array. 1)在C中,每当在带有指针的上下文中使用数组变量名时,它“意味着”指向数组的第一个元素的指针。 So arrays and pointers are often thought to be interchangeable. 因此,数组和指针通常被认为是可互换的。

2) In C, a function parameter of type char[] is not in fact an array. 2)在C中,类型的函数参数char[]实际上不是阵列。 It's just a pointer, exactly the same as char* . 它只是一个指针,与char*完全相同。 So, again, arrays and pointers are often thought to be interchangeable. 因此,数组和指针通常被认为是可互换的。

So, another difference between the pointer and the array: 那么,指针和数组之间的另一个区别是:

string1 = "bar"; // changes string1 to point to another string literal.

string1 = string2; // changes string1 to point to the first character of string2.

string2 = string1; // doesn't compile - you can't assign to an array,
                   //   only initialize it and then modify element-by-element.

[Note: the declaration char string2[]; [注意:声明char string2[]; in the question is not valid C syntax in a function, but the definitions I've used would be valid either in a function or at file scope, outside any function. 在问题中,函数中的C语法不是有效的,但我使用的定义在函数或文件范围内,在任何函数之外都是有效的。 Either way they behave as I've described for initialization and assignment, but they have different lifetimes.] 无论哪种方式,它们的行为与我所描述的初始化和赋值一样,但它们的生命周期不同。]

They are different in the way of the internal representation, but almost similar to way of working for the programmer. 它们在内部表示方式上有所不同,但与程序员的工作方式几乎相似。

While char string2[]; char string2[]; is an array of characters, when assigned directly, the compiler will understand that it's a collection of characters, making sizeof() return the size of the array (and not the string). 是一个字符数组,当直接分配时,编译器将理解它是一个字符集合,使得sizeof()返回数组的大小(而不是字符串)。

char *string1 is a pointer to the first character in a string. char *string1是指向字符串中第一个字符的指针。 It contains no compile time information about what it holds and will return the size of a pointer (4 on 32bit, 8 on 64bit) when queried with sizeof. 它不包含有关它所包含内容的编译时间信息,并且在使用sizeof查询时将返回指针的大小(32位为4位,64位为8位)。 Pointers posses built-in operator[] (overridable in C++) that make them act as arrays. 指针具有内置的operator[] (在C ++中可以覆盖),使它们充当数组。

Pointers are more flexible and can change to what content they point to, while arrays cannot. 指针更灵活,可以更改为指向的内容,而数组则不能。 The only way you can fill arrays is by using strcpy, memcpy or similar manual copy. 填充数组的唯一方法是使用strcpy,memcpy或类似的手动副本。 Pointers can be freely assigned to the first and even Nth element of any memory location to have more flexibility over the content. 指针可以自由分配到任何内存位置的第一个和第N个元素,以便在内容上具有更大的灵活性。

1) *char string1; 1)* char string1; is a pointer to (possibly) a string whereas 是指向(可能)字符串的指针

2) char string2[]; 2) char string2 []; is more explicit in that there is an intention to point to an array 更明确的是,有意指向一个数组

Either way, you still need to allocate memory to hold the said string. 无论哪种方式,您仍然需要分配内存来保存所述字符串。 I would go with #2 from an elegance point of view. 从优雅的角度来看,我会选择#2。

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

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