C String Literals and Pointers Assignment [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)
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.

Related

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 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"};

C error about pointer [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.
Can anyone explain why this code not work, pls!! Thanks you so much!
#include <stdio.h>
void changer(char * tp)
{
int i=0;
tp[0]='b';
}
int main(void)
{
char *st="aaab";
changer(st);
printf("%s",st);
}
This statement
tp[0]='b';
results in undefined behaviour because tp points to a string literal. You are not allowed to modify a string literal in C.
Instead you could use an array:
char st[] = "aaab";
which you'd be able to modify.
char *st="aaab";
This statement indicates that st is a pointer variable but "aaab" is string constant.
Instead try
char st[]="aaab";
This statement indicates that it declares st as array [5] of char and copies the contents of the string literal. Here st is a constant pointer.

Resources