C pointer arithmetic confusion [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 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.

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.

C String Literals and Pointers Assignment [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)
Is it possible to modify a string of char in C?
(9 answers)
Closed 3 years ago.
I was looking at tutorials for C and pointers on the internet and found a debugging example and am wondering how to fix this block of code? I have been looking for a while and can't find out how to make it work. I want to replace the 'i' in "Harris" with an "a".
char * ptr = (char *) "Harris";
ptr[4]="a";
While you can assign a constant to a char pointer, you can't normally write to it. Fix your code:
char ptr []= "Harris";
For not-your-legacy code use -fwritable-strings.
ptr[4]='a';
Single quotes for character constants.

I can't seem to know how to concatenate two char pointer strings using strcat, is it not correct? [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 can't concatenate two pointer strings using strcat, is it not possible?
I tried using them like strcat(s1,s2), and strcat(*s1,*s2), and all and it still doesn't work.
char *s1="Hello";
char *s2="Bye";
printf("%s\n",s1);
strcat(s1,s2);
printf("%s",s1);
When I run it prints the first "Hello" that is before the strcat, but the code doesn't display the remaining output and doesn't return 0.
Your approach cannot work, for several reasons:
char *s1="Hello";
s1 points to a read-only string (literal). You cannnot modify it.
strcat(s1,s2);
This cannot work because there is not enough room in s1 to add s2.
Use:
char s1[30]="Hello";
char *s2="Bye";
strcat(s1,s2);
With char s1[30]="Hello"; the compiler allocates an array for 30 charactes and then copies the string "Hello" into that array. Unused elements are set to zero.
With char *s2="Bye"; the compiler makes s2 point to a read-only string, so to make that explicit it is better to write const char *s2="Bye";

Why does this code crash at "temp[2]='X'? [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 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";

Bus error 10 in C: string literals [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 7 years ago.
Can someone explain why
char *s1 = "abcd";
char *s2 = s1;
s1[0] = "z";
s1[2] = "\0";
gives me a bus error 10 BUT
char s1[] = "abcd";
char *s2 = s1;
s1[0] = "z";
s1[2] = "\0";
doesn't?
are char *s1 and char s1[] not equivalent? Please explain, thanks.
Be free (of historical lazyness), be wise! Pointers are not arrays! Tutorials have lied you!
In the first example, you're modifying a pointer to a constant string literal, and that's undefined behaviour. Anything can happen then!
Meanwhile, in the second case, the string itself is stored inside the array, and the array itself is in the stack. Thus, the second example exposes more than a plain innocent array that's modifiable.
The s2 pointers make no difference in all this. IMHO, the fact that the first case is compilable is just historical lazyness, otherwise known as backwards compatibility.
BTW: Are you assigning string literals to chars? That's undefined behaviour too!
In the first case you set a s1 pointer to the address const string. Const string are stored in read -only area and you cannot modify it. This means that you cannot modify a character s[x]. It is UB
In the second case you declare a local array inited with a string. In this case only the init value is read-only and after init you use an allocated array that can be modified.

Resources