Segmentation Error in C Code [duplicate] - c

This question already has answers here:
Why can't I use this code to overwrite a string?
(4 answers)
Closed 9 years ago.
I am totally new to C and I am about to write a function that reverses a string. My code looks like this:
char *str = "abcdef";
char *ptr;
for(ptr = str; *ptr ; ptr++);
for(; str < --ptr; str++)
{
char c = *str;
*str = *ptr;
*ptr = c;
}
I get a segmentation fault error. And I don't see the mistake (maybe it is too obvious). Any hints?
Cheers

Change
char *str = "abcdef";
to
char str[] = "abcdef";
The first str points to a string literal and string literals are not modifiable in C,

Your string is stored in ROM, so you can't write to it. Depends on your platform though.

Related

Copy string in C, using 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 4 years ago.
I am a beginner programmer. I wrote the following code to copy the one string into other, using pointers.
But I am not getting the output. The compiler says segmentation fault.
I have gone over and over the program, but to no avail. I am not able to locate the fault, and how to fix it.
It would be hard to believe but I have been stuck for almost 2 hours now.
Any help is greatly appreciated.
#include<stdio.h>
char *copy(char*, char*);
int main() {
char *str1 = "avanti";
char *str2 = "ujj";
printf("%s\n", str1);
char *result = copy(str1, str2);
printf("%s", result);
}
char *copy(char *str1, char *str2){
int i=0;
while (*(str2+i) != '\0') {
*(str1+i) = *(str2+i);
i++;
}
*(str1+i) = '\0';
return str1;
}
"avanti" is a string constant, not a place you can copy to. You might change it to char str1[] = "avanti"; This is an array of characters that is initialized with the value of the string constant "avanti"

sprintf crashes but it is unclear why [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.
I tried the following code with sprintf, but it crashes in some cases and works fine in the other. Could anyone explain it to me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
//char *s = malloc(20); //works fine
//char *s = ""; //does not work, no matter what the initial value is
char s[20]; //works fine
sprintf(s, "%s", "hello world");
printf("%s",s);
return 0;
}
When you are doing this:
char *s = "";
or
char *s = "longer string";
You are creating a literal string which is possibly placed in read-only memory. So you cannot change it later.
If you try to do the following syntax:
char s[] = "...";
the array will be initialized with the literal string and you will be able to modify the original array later.
Suggested questions on site:
What is the difference between char s[] and char *s in C?
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”?

C : Character swapping [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 10 years ago.
I need to swap two characters by pointers but when I run this code,the program crashes.
int main(){
char *s1 = "string1";
swap(st,(st+1));
/* BUT THIS CODE WORKS - Whats the problem?
* char s1[] = "string1";
* swap(s1,&s1[1]);
*/
return 0;
}
void swap(char * const ptr1, char * const ptr2){
char temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
char *s1 = "string1";
Because s1 points to a string literal and modifying invokes undefined behaviour in C. That's why this doesn't work.
Whereas in this char s1[] = "string1";
s1 is an array and hence it can be modified.
A string literal may not be modified. You try to modify "string1" in your code which is not allowed. Indeed, many compilers place string literals in a special section that may not be written into.
This line of code creates a string literal which cannot be changed. It is only readable.
char *s1 = "string1";
Any attempt to change it will give you an error.
While your commented example :
char s1[] = "string1";
creates an actual array. This can be edited and used normally.

Reverse C string - simple bug [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to modify a string of char in C?
#include <stdio.h>
void reverseStr(char *str);
main()
{
reverseStr("abcdef");
}
void reverseStr(char *str) {
char *tmp = str;
char curr;
while (*tmp != '\0') {
tmp++;
}
tmp--;
while (tmp > str) {
curr = *str;
*str = *tmp;
*tmp = curr;
str++;
tmp--;
}
}
When I run it, I get:
/usr/bin/runit/srun_c: line 12: 2809 Segmentation fault /tmp/run_c_executable
What on earth is going on? I'm practicing for an interview, I'm rusty in my C and wanted to practice something easy but can't for the life of me figure this out.
I've noticed the seg fault disappears when I comment out the *str = *tmp; line, and I don't see why that should cause a seg fault.
Help appreciated.
You can't modify constant strings. Use a char array instead:
char str[] = "abcdef";
reverseStr(str);
You can't modify string literals — they're stored in readonly memory.
Use:
char str[] = "abcdef";
reverseStr(str);
Your reversal function looks fine. But it is the way you are calling the function that is causing this crash. You are passing a string literal which are read-only to the function. And modifying a string literal is a undefined behavior, manifesting as crash in your case.
Change
reverseStr("abcdef");
to
char str[] = "abcdef";
reverseStr(str);
where you pass a character array to the function.

Replace individual character element of a string C [duplicate]

This question already has answers here:
How to replace specific characters in a string with other characters
(3 answers)
Closed 9 years ago.
I am trying to do something really basic on C but I keep getting a segmentation fault. All I want to do is replace a letter of a word with a different letter- in this example replace the l with a L. Can anyone help explain where I have gone wrong? It should be a really basic problem I think, I just have no idea why its not working.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
char *string1;
string1 = "hello";
printf("string1 %s\n", string1);
printf("string1[2] %c\n", string1[2]);
string1[2] = 'L';
printf("string1 %s\n", string1);
return 0;
}
For my output I get
string1 hello
string1[2] l
Segmentation fault
Thanks!
string1 = "hello";
string1[2] = 'L';
You can't change string literals, it's undefined behavior. Try this:
char string1[] = "hello";
Or maybe:
char *string1;
string1 = malloc(6); /* hello + 0-terminator */
strcpy(string1, "hello");
/* Stuff. */
free(string1);
char *string1;
string1 = "hello";
string1 points to a string literal and string literals are non-modifiable.
What you can do is initialize an array with the elements of a string literal.
char string1[] = "hello";
the elements of string1 array are modifiable.
char *string1 = "hello";
When running the code, the string literal will be in a section that is read-only. OS does not allow the code to change that block of memory, so you get a seg-fault.
char string1[] = "hello";
The string literal will be pushed onto the stack when you run the code.
string1[2] = 'L';
you are trying to change a string literal which is undefined behavior in C.
Instead use string1[]="hello";
Segmentation fault you get is because the literal is probably stored in the the read only section of the memory and trying to write to it produces undefined behavior.

Resources