Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:17:50+00:00 2026-05-26T09:17:50+00:00

I am following an example of a book in writing a function to reverse

  • 0

I am following an example of a book in writing a function to reverse a string in C.
This is the following problem. But when I execute it under ubuntu using gcc. I get a seg fault.
I have tried debugging it, but I don’t understand how can this line ‘*start++ = *end;’ causing a seg fault.

I appreciate someone can help me understand the seg fault.

#include <stdio.h>
#include <stdlib.h>

void myreverse(char* str) {
    int len = strlen(str);
    char tmp;

    char* start = str;
    char* end = str + (len -1);

    while (start < end) {
        tmp = *start;
        // this is causing Segmentation fault
        *start++ = *end;
        *end-- = tmp;
    }
}

int main(void) {
    char* test = "Hello World";
    puts(test);
    myreverse(test);
    puts(test);
    return EXIT_SUCCESS;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:17:51+00:00Added an answer on May 26, 2026 at 9:17 am
    char* test = "Hello World";
    

    when you dynamically give a string to a character pointer, the string you give gets saved in Read only part of data segment..Then you are passing this address to your myReverse function where you are trying to change the contents of that location in the line

    *start++ = *end;
    

    which cause the segmentation fault..

    Just change this line

    char* test = "Hello World";
    

    to

    char temp[] = "Hello world";
    char *test  = temp;
    

    and it wouldn’t crash..See this thread for more details..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a simple example from Odersky's book resulted in the following problem: // AbstractElement.scala
I'm following the racetrack example from Jason Rudolph's book at InfoQ , using grails-1.2.1.
I was trying the following example, but with external URLs: Using WebViews The example
In book named Using C++ by Rob McGregor there is following example of using
i run the following example from book c++ concurrency in action, and pass function
For class specific new_handler implementation, i came across the following example in book effective
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Let me use the following example to explain my question: public string ExampleFunction(string Variable)
I was trying learn lex and yacc using the oreilly book. I tried following
Consider the following example from APUE book: #include <errno.h> #include <sys/socket.h> int initserver(int type,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.