简体   繁体   中英

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?

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).

char string2[] = "foo";

string2 is an array of 4 characters. It is initialised with the bytes 'f', 'o', 'o', ASCII_NUL.

Probably the most significant difference is that if you do 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'; then you modify your personal string to "boo", which is fine.

In general, a variable of type char* is a pointer to a 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. A variable of type char[] is an array of chars. If it has a NUL terminator, then it actually is a string.

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. 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. It's just a pointer, exactly the same as 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[]; 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. 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[]; 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).

char *string1 is a pointer to the first character in a string. 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. Pointers posses built-in operator[] (overridable in C++) that make them act as arrays.

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. Pointers can be freely assigned to the first and even Nth element of any memory location to have more flexibility over the content.

1) *char string1; is a pointer to (possibly) a string whereas

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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