Why does this code crash at "temp[2]='X'? [duplicate] - c

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 7 years ago.
int main(void) {
char *temp = "ABCDE";
temp[2] = 'X';
printf("%s", temp);
return 0;
}
In gdb I could see that temp[2] = 'X' is causing the crash. But why? Could some answer please?

String literals are non-modifiable. It may be stored in read-only section of the memory and any attempt to modify it will lead to undefined behavior.
If you want to modify it, declare temp as an array of chars
char temp[] = "ABCDE";

Related

Why are strings declared as an array "char word[5] = "hello" modifiable but not strings declared as pointer (char *word = "hello")? [duplicate]

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 3 years ago.
I was under the impression that
char word[5] = "hello";
is the same as
char *word = "hello";
because arrays always decay into a pointer?
char *word = "hello"; Takes the address of a string literal. These are often held in read-only memory.

why is assigning '\0' causes segmentation fault? [duplicate]

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 3 years ago.
I've tried this code snippet to assign '\0' to the last character of a C string
char *words = "words";
words[4] = '\0';
why is line 2 causes segmentation fault?
You didn't allocate the space for "words" (you only allocated the pointer to it), so (apparently) it has been placed some where you are not allowed to modify.

C pointer arithmetic confusion [duplicate]

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 4 years ago.
I have to answer if the following code compiles and which are the results:
char *s1 = "A String";
char *s2 = "Other String";
*s1 = *s2;
I didnt really find what happens in the background when you do declarations like that. Is s1 pointing to memory looking like this?:
|A| |S|t|r|i|n|g|\0|
In my understanding *s1 = *s2 is the same like s1[0] = s2[0], right?
So why do I get a memory error?
Shouldnt it be?:
|O| |S|t|r|i|n|g|\0|
Literals strings in C are really read only arrays of characters, and can (and should) not be modified.
Attempting to modify the contents of a string literal leads to undefined behavior.
Always make it a habit to use const char * when having pointers to string literals.

Segmentation fault in c array, access to individual char in a string [duplicate]

This question already has answers here:
segmentation fault in C by replacing a string's letter [duplicate]
(4 answers)
Modifying String Literal [duplicate]
(4 answers)
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 4 years ago.
I am attempting to manipulate individual characters in a string, in this case change 4th 'a' to a 'b'.
string password = "aaaaa";
printf("password: %s\n",password);
int j = 'b';
password[3] = (char) j;
printf("password: %s\n",password);
this returns:
password: aaaaa
Segmentation fault
One last note: in the first line I declare 'string' like a variable. This contrivance is allowed by the CS50 library - It should work and I have used it in the past.
Thank you in advance.
"aaaaa"; is a String Literal which is immutable on most systems, so password[3] = (char) j; attempts of modify an immutable object resulting in a SegFault.
Instead,
char password[] = "aaaaa";
Presuming your "string" is a typedef of char* using a compound literal allows the same result, e.g.:
string password = (char[]){"aaaaa"};

Why is assigning a character in a string to itself a bus error? [duplicate]

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 6 years ago.
This works and produces bbcd as I'd expect.
#include <stdio.h>
int main(void) {
char string[] = "abcd";
string[0] = string[1];
printf("%s\n", string);
}
This is a bus error.
#include <stdio.h>
int main(void) {
char *string = "abcd";
string[0] = string[1];
printf("%s\n", string);
}
Why?
Valgrind says:
==9909== Process terminating with default action of signal 10 (SIGBUS)
==9909== Non-existent physical address at address 0x100000FA2
==9909== at 0x100000F65: main (test.c:6)
Because in the second case, you're trying to modify a string literal which invokes undefined behavior.
To elaborate, in the second case, string[0] is the first element of the string literal, and any assignment to that is an attempt to modify the value held by that element.
Quoting C11, chapter ยง6.4.5, "String literals"
[...] If the program attempts to modify such an array, the behavior is
undefined.

Resources