This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Is it possible to modify a string of char in C?
What is the difference between char s[] and char *s in C?
There is something I don't understand about strings and pointers in C.
Suppose I have this declaration:
char str[] = "abc";
Then, if I attempt to modify it this way:
str[0] = 'b';
It will work.
But if I declare the string as a pointer to a char
char* str = "abc"
The attempt above will cause an access violation.
What I'm trying to understand is what exactly is the difference.
Thanks in advance
In The later example the compiler puts the string in the read only data section, so u can't modify it. But in earlier example you are declaring array of character which resides in stack hence you can modify content of this array.
Related
This question already has an answer here:
Why can I not modify a string literal in c?
(1 answer)
Closed 4 months ago.
for example:
char stringer[]="hello";
stringer[2]='A';
The above works to change 'l' to 'A'. But if I do the following:
char *stringer="hello";
stringer[2]='A';
This doesn't work, is there a reason for this?
As answered by Some programmer dude in comments:
Literal strings are really non-modifiable arrays of characters. With char *stringer="hello"; you make stringer point to the first character of such an array. Attempting to modify its contents leads to undefined behavior.
That's why you should always use const char * when pointing to literal strings.
Strings can be modified when using pointer, if the pointer is pointing to something you're allowed to modify. For example
char stringer[] = "hello";
char *pointer = stringer;
pointer[2] = 'A';
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 8 years ago.
I'm currently learning C and am having a bit of trouble understanding how to declare a string. I understand that in C, strings are simply arrays of characters. I've seen two different ways for declaring a string:
char[] some_string = "hello world";
and
char *some_string = "hello world";
When should each of these be used? Most code I've seen uses the char *some_string syntax, but I'm sure that there's a reason why. Thanks.
char some_string[] = "foo" declares a local array of size 4 which is initialized at run time with the values 'f', 'o', 'o', and '\0'. That memory is (probably) on the stack of the function. char *some_string = "foo" declares a pointer which is initialized to the location of a string which is initialized at compile time to the same values. The string char [] some_string = "foo" is a syntax error. The most relevant difference between the two is that, on many platforms, the latter is not writable. In other words, some_string[1] = 'a' will generate a segfault on some platforms if some_string was declared as a pointer rather than an array.
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 9 years ago.
In my GCC 32-bit compiler, the following code gives the output
char *str1="United";
printf("%s",str1);
output:
United
then should I consider char *str1="United"; the same as char str1[]="United"?
The two are not the same: char *str1="United" gives you a pointer to a literal string, which must not be modified (else it's undefined behavior). The type of a literal string should really be const but it's non-const for historical reasons. On the other hand, char str1[]="United" gives you a modifiable local string.
char* str = "United"; is read-only. You wouldn't be able to reach inside the string and change parts of it:
*str = 'A';
Will most likely give you a segmentation fault.
On the other hand char str1[] = "United"; is an array and so it can be modified as long as you don't exceed the space allocated for it (arrays cannot be resized). For example this is perfectly legal:
char str[] = "United";
str[0] = 'A';
printf("%s\n", str);
This will print Anited.
See the comp.lang.c.faq, question 1.32. It basically boils down to the fact that declaring the string in array form (char str[] = "foo") is identical to char str[] = {'f','o','o'}, which is identical to char str[] = {102, 111, 111}; that is, it is a normal array on the stack. But when you use a string literal in any other context it becomes "an unnamed, static array of characters, [which] may be stored in read-only memory, and which therefore cannot necessarily be modified." (And trying to modify it results in undefined behavior wherever it happens to be stored, so don't).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it possible to modify a string of char in C?
char *s = "anusha";
Is this like a constant pointer? When i tried to change the character in location 3 by writing s[3]='k', it gave me a segmentation fault. So i am assuming it is like pointing to a constant array or s is a constant pointer? Which among the two? Please clarify.
That is correct, you are not allowed to modify string literals.
However, it's legal to do this:
char s[] = "anusha";
s[3] = 'k'
The difference here being that it is stored as a local array that can be modified.
It looks like your compiler treats "anusha" as a pointer to char, but places the string itself into write-protected memory. I remember reading that this is a convenience policy in order to comply with existing code.
As Joe pointed out, this is detailed in Is it possible to modify a string of char in C?.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between char s[] and char *s in C?
More of a general question rather than trying to fix something, I've been reading the C programming language book and they take care to make the distinction between
char amessage[] = "blah";
char *pmessage = "blah";
The difference being one is a char array and the other a pointer to a string constant. They say modifying the char array is acceptable but you shouldn't modify string constants as it triggers undefined behavior. My question is: isn't the string constant stored in memory the same way the char array is? Why can I modify it as in
char *p = "this is a string constant";
*(p+2) = 'a';
printf("%s", p);
Ends up printing "thas is a string constant" as you might expect. I can understand how it would make sense as a string constant shouldn't end up being changed at run time, as it might confuse others/yourself working on your code not expecting it's value to change but in purely functional terms what is wrong with it, what is the undefined behavior that might trigger and how mechanically could it backfire when a char array wouldn't? I'm just wondering if I'm missing something about how string constants work in memory and how they are seen by the compiler.
At least on my computer, the following program crashes:
#include <stdio.h>
int main() {
char *p = "this is a string constant";
*(p+2) = 'a';
printf("%s", p);
}
If it appears to be working for you (which it might on certain embedded compilers), you're just getting lucky. Undefined behavior means the program is allowed to do anything. See http://blog.regehr.org/archives/213 .
See also What is the difference between char s[] and char *s?.
In case of char array the content of the string literal "blah" are copied on to the stack as well. So you can modify it without invoking UB because after all its just a copy.
In case of char * you actually try to modify the original string literal and as per the Standard that is UB.
With a string constant, there's not guarantee about where the data will be stored -- the compiler is free to do any tricks it wants, because you're supposed to be forbidden from writing to it. So for example, it's not unheard of for the pointer to actually point to the spot in the loaded executable code itself where the string constant is defined.