This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Why does char* cause undefined behaviour while char[] doesn’t?
The following code
int main() {
char * st = "abc";
*st = 'z';
return 0;
}
is returning a segmentation fault. If the strings on the stack are not modifiable why is it not giving the error at compile time?
The variable on the stack, st, is a pointer. The value assigned is to a string constant (read-only).
char *str = "this is dangerous to modify"; is not a string in the same sense you get; it's called a string-literal and modifying it produces undefined behavior according to the standard.
If you want a string that you can later modify, go like this:
char str[] = "Some String";
then modify it accordingly.
Related
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 2 years ago.
I have the following code:
int main()
{
char *input = "why isn't this possible?";
input[0] = 'W';
return 0;
}
I want to modify the first value of the string, but it appears to cause an access violation on the input[0] = 'W'; line.
Any ideas why this is happening. Oddly I don't recall this error happening on my old machine running Visual Studio, but happens with GCC and Pellas C.
First of all, what your attempting to modify is not a string, it's a string literal.
Yes, you see different behaviour in different cases because this attempt is mentioned to exhibit undefined behaviour in C standard.
Quoting C11, chapter 6.4.5, String literals
[...]If the program attempts to modify such an array, the behavior is undefined.
To achieve the expected output, you can simply use an array, initialized by the string literal and them attempt to modify the array element. Something like:
int main(void)
{
char input[ ] = "why isn't this possible?"; // an array
input[0] = 'W'; // perfectly modifiable
return 0;
}
Any attempt to modify a C string literal has undefined behaviour. A compiler may arrange for string literals to be stored in read-only memory (protected by the OS, not literally ROM unless you're on an embedded system). But the language doesn't require this; it's up to you as a programmer to get it right.
Try this if you want the functionality:
int main()
{
char input[] = "why isn't this possible?";
input[0] = 'W';
return 0;
}
Otherwise to learn more on it, check this answer out:
Char* and char[] and undefined behaviour
This question already has answers here:
Modify a string with pointer [duplicate]
(4 answers)
Closed 5 years ago.
I am getting segmentation fault when running the below code. What could be the reason for this error? Please help
int main()
{
char *str2 = "Hello";
str2[3] = 'J';
printf("%s\n",str2);
return 0;
}
It is a undefined behaviour because you are trying to modify the content of a string literal. A string literal mainly stored in a read only location. so you do not modify it, otherwise it is invoked undefined behaviour.
C11 §6.4.5 String literals(Paragraph 7):
It is unspecified whether these arrays are distinct provided their
elements have the appropriate values.If the program attempts to
modify a string literal of either form, the behavior is undefined"
You aren't allowed to modify a string constant, and in this case it's causing a runtime error. You can fix it by changing the declaration of str2 to:
char str2[] = "Hello";
This makes it an array, rather than a pointer to a string constant.
You are not allowed to modify the memory pointed to by char* variables initialized with string literals. It is read-only.
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 10 years ago.
Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;
char *str = "Hello";
printf("%c",++*str);
This gives segmentation fault on linux with gcc.
The moment the first statement is changes to as
char str[10] = "Hello";
It works. What may be the reason?
It is undefined behaviour to attempt to modify a string literal.
The compiler is free to place it in read-only memory (as it probably does in your case). Attempting to modify read-only memory is what's probably triggering the segfault.
This statement char *str = "Hello"; stores the string "Hello" in RO-section and assigns the address of the area of RO-section(in which "Hello"is stored) to str. The data stored in RO-section cannot be modified thus you are getting a segfault.
char str[10] = "Hello";
is also wrong. You should instead write
char str[10];
strncpy(str,"Hello",sizeof(str));
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.