Segmentation Error in C Code

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

Solutions

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.

Similar questions

Error occurs when running a dynamic test: "run dynamic test: error::Error: Exit code with code: 1 and signal: null"
My objective is to sort out an array of numbers that are in a txt file called "3_1.txt". I have implemented code in C Lang to sort the numbers out called "sort.c". This is an assignment for school I have been working on but cannot seem where I am going wrong. The only reason I think something is NOT correct is because on the Git...
I keep getting segmentation errors when I pass the capitol H flag through the command line, can anyone spot the segmentation error?
this is my main class that accepts a h or H command line parsing and should print the contents of a hexidecmal header I have a program that takes in command line arguments and a path to an elf file and displays the content of them using a struct. The elf header file is set up to have 16 bytes. Of those bytes, every two bytes describes something els...
C
What code is user mode code and what code is kernel mode code?
I am having a question for myself while taking operating system course. If I type in any C code to my text editor or though IDE and execute it with the compiler it translates the code into machine code. Then I would guess if I run the program, the OS will allocate a memory address to the code which is done by kernel code. And if there was IO interr...
Is extern "C" turning C code to C++ code, or just allowing the compiler to compile C code as C++ code?
I have a small issue. I thought that using using extern "C" will turn the C code into C++ code directly. But when I placed a class QSerialPort in here Then I got 500 errors about data types, name space and all kind of C++ keywords that C does not have. Question: In QT. I have to make sure that I can use a C++ class inside a .h file and th...
C++ C
The code is supposed to transform every letter of every word to uppercase. But running the code results in a bus error. What causes the bus error?
The code is supposed to transform every letter of every word to uppercase. But running the code results in a bus error. What causes the bus error? Input: sEbas Output: SEBAS
How can I detect unterminated comment and write an error message saying "Error: line X: unterminated comment" to the standard error stream in my code?
This is a simple de-commenting C program (which is C pre-processor job). I was struggling adding a feature to write an error message when detecting unterminated comment (/* example) to the standard error stream. The error should say something like Error: line X: unterminated comment where X is the line number the error occurred. I have been trying ...
C

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.