1

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?

Community
  • 1
  • 1
rogi
  • 93
  • 2
  • 9

5 Answers5

3

In C, strings are represented as sequences of chars, 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.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • With other words the first declaration creates immutable string while the second one mutable ? – rogi Aug 25 '12 at 12:04
  • No, it *is* possible to change the string if you want (you can do nearly anything in C). But you *must not*! They are just different ways of creating a string. You should read about memory allocation to understand better. – Joe Aug 25 '12 at 12:05
  • +ten thousand for " you can do nearly anything in C, *But you must not!* " – Chani Aug 25 '12 at 12:09
  • I'd rather you clicked the upvote 9,999 times than 10,000! – Joe Aug 25 '12 at 12:15
  • Can downvoters please give reasons for downvoting? – Joe Aug 27 '12 at 17:31
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.

Joe
  • 46,419
  • 33
  • 155
  • 245
perilbrain
  • 7,961
  • 2
  • 27
  • 35
  • Can you explain where I said that `char *str = ".."` is runtime allocation? I think you misread my answer (by mistake or deliberately). – Joe Aug 27 '12 at 12:40
0

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";
Joey
  • 344,408
  • 85
  • 689
  • 683
0

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

0

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
rashok
  • 12,790
  • 16
  • 88
  • 100