问题
Possible Duplicate:
What is the difference between char s[] and char *s in C?
Question about pointers and strings in C
I'm reading about the strings in C and I'm confused. I can "declare" strings in two ways:
char *str = "This is string";
char str2[20] = "This is string";
What is the difference between the two declarations? When would char str2[20]
be preferred over char *str
?
回答1:
char *str = "This is string";
Puts the string in the constant data section (also known as .rdata)
of the program.This data can't be modified.
char str2[20] = "This is string";
In this type of declaration data is preferably stored in the stack area
of the program, if declared inside the function scope
and in data section
if declared in global scope
.This data can be modified.
So if you have a necessity to modify data then use the second approach.
回答2:
In C, strings are represented as sequences of char
s, with a NULL character (aka 0, '\0'
). They are stored in memory and you work with a way of referencing it. You have identified the two ways of referencing it, a char *
, which is a pointer to a sequence of chars
and an array, which is an immediate string of chars as an actual variable. Be aware that the string "abc" is 4 bytes long as there is an additional NULL character to represent the end of the string.
In addition to this, you are actually assigning strings in the example, which also involves the strings to given at compile-time.
So two questions. First is about how you represent strings (char *
vs char[]
) the second is about compile-time strings.
To come to your examples:
The first one creates a constant string in the text of the program and a pointer to it. Depending on the compiler it may be stored anywhere. It is the equivalent of mallocing
a string and storing a pointer to it, except you must not change the contents of the memory. It is a char *
, so you can change the pointer to point to somewhere else, like another malloced
string or to the start of an array that you defined in example 2.
The second creates a char array (which a way of representing a string). The array is stored and allocated on the stack for the duration of the function, and you may change the contents. Because it is not a pointer, you cannot change it to point to a different string.
回答3:
C has no strings. All there is is char
arrays. And arrays in C are just pointers to the first element.
The easiest way of doing it is in fact your first variant. Not specifying an explicit length of the array for literals will save you from accidentally doing something like
char[3] = "abc";
回答4:
C strings are constant in memory, so:
char *str = "This is string";
Stores "This is string" in memory and it is not mutable, you can only assign another address to str.
However
char str2[20] = "This si string";
is a shorthand of
char String2[20]={'T','h','i','s',' ','s','i',' ','s','t','r','i','n','g','\0'};
which does not stores a string in memory, stores independent bytes.
If you want to use constant strings like messages, then use first line. If you want to use and manipulate strings like in a word processor then use second.
Regards
回答5:
char *str = "This is string";
- This will keep the string in text segment as a read only data and it will store the address in the local pointer variable str
.
str[0] = 'a'; //This will leads to crash, because strings are in read only segment.
printf("%d",sizeof(str)); //This will print 4(in 32bit m/c) or 8(in 64 bit m/c)
char str2[20] = "This is string";
- This will keep the string as character array in local stack.
str2[0] = 'a'; //This will change the first character to a
printf("%d",sizeof(str2)); //This will print 20
来源:https://stackoverflow.com/questions/12121759/difference-between-char-str-and-char-strn