Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 trong 5
http://stackoverflow.com/questions/1704407/what-is-the-difference-betwee...
Stack Overflow is a question and answ er site for professional and enthusiast programmers. It's 100% free, no
registration required.
Tell me more
string
char
constants
9 Answers
The difference here is that
char *s = "Hello world";
will place Hello world in the read-only parts of the memory and making s a pointer to that, making any
writing operation on this memory illegal. While doing:
char s[] = "Hello world";
puts the literal string in read-only memory and copies the string to newly allocated memory on the stack.
Making
s[0] = 'J';
legal.
edited Nov 9 '09 at 22:46
4 The literal string "Hello world" is in "read-only parts of the memory" in both examples. The
example with the array points there, the example with the array copies the characters to the array
elements. pmg Nov 9 '09 at 22:42
11/16/2013 7:30 AM
string - What is the difference between char s[] and char *s in C? - Stack...
2 trong 5
http://stackoverflow.com/questions/1704407/what-is-the-difference-betwee...
5 pmg: In the second case the literal string does not necessarily exist in memory as a single contiguous
object at all - it's just an initialiser, the compiler could quite resonably emit a series of "load
immediate byte" instructions that contain the character values embedded within them. caf Nov 9
'09 at 22:46
3 The char array example does not necessarily place the string on the stack - if it appears at file level,
it will probably be in some kind of initialised data segment instead. caf Nov 9 '09 at 22:47
1 @caf, @Charles: The Standard (n1401.pdf) says @ 6.4.5 String literals /5 "... The multibyte
character sequence is then used to initialize an array of static storage duration and length
just sufficient to contain the sequence. ..." It doesn't "speak" of 'plain' character sequences,
but I think the same applies. I also think an implementation can ignore this particular bit of the
Standard for performance reasons :) pmg Nov 9 '09 at 23:00
2 I'd like to point out that char s = "xx" doesn't have to be in read-only memory (some implementations
have no MMUs, for example). The n1362 c1x draft simply states that modifying such an array causes
undefined behavior. But +1 anyway, since relying on that behavior is a silly thing to do. paxdiablo
Nov 10 '09 at 12:35
show 3 more comments
2 I think you meant x[1] = 'O'; (single quotes, not doubles). paxdiablo Nov 10 '09 at 12:37
Indeed I did, fixed. bdonlan Nov 10 '09 at 17:05
This declaration:
char s[] = "hello";
Creates one object - a char array of size 6, called s , initialised with the values 'h', 'e', 'l', 'l',
'o', '\0' . Where this array is allocated in memory, and how long it lives for, depends on where the
declaration appears. If the declaration is within a function, it will live until the end of the block that it is
declared in, and almost certainly be allocated on the stack; if it's outside a function, it will probably be
stored within an "initialised data segment" that is loaded from the executable file into writeable memory
11/16/2013 7:30 AM
string - What is the difference between char s[] and char *s in C? - Stack...
3 trong 5
http://stackoverflow.com/questions/1704407/what-is-the-difference-betwee...
1 In both declaration for "hello" memory is allocated at comiple time ?.And another thing char *p =
"hello" here "hello" is stored in text segment as you stated in your answer...and what about char s[] =
"hello" will it also store first in text segment part and during run time it will copy in stack as Rickard
has stated in there answer. please clarify this point. Nishant May 16 '12 at 15:00
2 @Nishant: In the char s[] = "hello" case, the "hello" is just an initialiser telling the compiler
how the array should be initialised. It may or may not result in a corresponding string in the text
segment - for example, if s has static storage duration then it is likely that the only instance of
"hello" will be in the initialised data segment - the object s itself. Even if s has automatic
storage duration, it can be initialised by a sequence of literal stores rather than a copy (eg. movl
$1819043176, -6(%ebp); movw $111, -2(%ebp) ). caf May 17 '12 at 1:28
Thanks caf for your clarification. Nishant May 17 '12 at 4:55
s0:
s1:
0x00008000:
0x00008004:
0x00008008:
...
0x00010000:
0x00010004:
0x00010008:
0x0001000C:
0x01
'h'
'o'
'r'
0x02
'e'
' '
'l'
0x03
'l'
'w'
'd'
0x04
'l'
'o'
0x00
0x00
'h'
'o'
'r'
0x00
'e'
' '
'l'
0x80
'l'
'w'
'd'
0x00
'l'
'o'
0x00
The string literal "hello world" is a 12-element array of char (const char in C++) with static extent,
meaning that the memory for it is allocated when the program starts up and remains allocated until the
memory terminates. Attempting to modify the contents of a string literal invokes undefined behavior.
The line
char *s0 = "hello world";
defines s0 as a pointer to char with auto extent (meaning the variable s0 only exists for the scope in
which it is declared) and copies the address of the string literal (0x00008000 in this example) to it. Note
that since s0 points to a string literal, it should not be used as an argument to any function that would try
to modify it (e.g., strtok() , strcat() , strcpy() , etc.).
The line
char s1[] = "hello world";
defines s1 as a 12-element array of char (length is taken from the string literal) with auto extent and
copies the contents of the literal to the array. As you can see from the memory map, we have two copies
of the string "hello world"; the difference is that you can modify the string contained in s1 .
11/16/2013 7:30 AM
string - What is the difference between char s[] and char *s in C? - Stack...
4 trong 5
http://stackoverflow.com/questions/1704407/what-is-the-difference-betwee...
.
char s[] = "hello";
declares s to be a array of char which is long enough to hold the initializer (5 + 1 char s) and
initializes the array by copying the members of the given string literal into the array.
char *s = "hello";
declares s to be a pointer to one or more (in this case more) char s and points it directly at a fixed
(read-only) location containing the literal "hello" .
edited Nov 9 '09 at 22:49
1 What method is preferable to use in functions if s will not be changed, f(const char s[]) or f(const char
*s) ? psihodelia Nov 8 '11 at 13:26
In the light of comments here it should be obvious that : char * s = "hello" ; Is a bad idea, and should be
used in very narrow scope.
This might be a good opportunity to point out that "const correctness" is a "good thing". Whenever and
wherever You can, use the "const" keyword to protect your code, from "relaxed" callers or programmers,
which are usually most "relaxed" when pointers come into play.
Enough melodrama, here is what one can achieve when adorning pointers with "const". (Note: One has to
read pointer declarations right-to-left.) Here are the 3 different ways to protect yourself when playing with
pointers :
const DBJ* p means "p points to a DBJ that is const"
that is, the DBJ object can't be changed via p.
DBJ* const p means "p is a const pointer to a DBJ"
that is, you can change the DBJ object via p, but you can't change the pointer p itself.
const DBJ* const p means "p is a const pointer to a const DBJ"
that is, you can't change the pointer p itself, nor can you change the DBJ object via p.
The errors related to attempted const-ant mutations are caught at compile time. There is no runtime
space or speed penalty for const.
(Assumption is you are using C++ compiler, of course ?)
--DBJ
edited Nov 9 '09 at 23:27
11/16/2013 7:30 AM
string - What is the difference between char s[] and char *s in C? - Stack...
5 trong 5
http://stackoverflow.com/questions/1704407/what-is-the-difference-betwee...
2 Technically, x is a non-modifiable lvalue. In almost all contexts though, it will evaluate to a pointer
to its first element, and that value is an rvalue. caf Nov 9 '09 at 23:02
Just to add: you also get different values for their sizes.
printf("sizeof s[] = %d\n",sizeof(s));
printf("sizeof *s = %d\n",sizeof(s));
As mentioned above, for an array '\0' will be allocated as the final element.
edited Oct 20 at 18:44
hopper
2,771 2 11 24
Not the answer you're looking for? Browse other questions tagged c string char
constants or ask your own question.
11/16/2013 7:30 AM