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 8966027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:56:10+00:00 2026-06-15T16:56:10+00:00

Possible Duplicate: Why is this C code causing a segmentation fault? I am writing

  • 0

Possible Duplicate:
Why is this C code causing a segmentation fault?

I am writing a simple string reverse script.

I added print statements to debug. I keep getting a run time exception before Error1. But I can’t seem to figure out the reason.

Here’s my code:

#include <iostream>
#include <cstdlib>

using namespace std;

int strlen(char* s){ 
  int i = 0;
  while(*s != '\0'){
    i++;
    s++;
  }
  return i;
}

void reverse(char* src){

  char* dest = src+strlen(src)-1;
  char temp;

  while(src < dest){
    temp = *src;
    cout << "Error0" << endl;
    *src = *dest;
    cout << "Error1" << endl;
    *dest = temp;
    cout << "Error2" << endl;
    src++;
    dest--;
  }

}

int main (void){

  char* s = "Hello world";
  cout << s << endl;
  int i = strlen(s);
  cout << i << endl;
  reverse(s);
  cout << s << endl;

  getchar();
  return 0;
}

Here’s my output:

            Hello world
            11
            Error0
  • 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-06-15T16:56:11+00:00Added an answer on June 15, 2026 at 4:56 pm

    this

    char* s = "Hello world";
    

    needs to be

    char s[] = "Hello world";
    

    Your orignal is trying to change constant memory that you aren’t allowed to change, so you need to allocate space and initialize it with the string

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

Sidebar

Related Questions

Possible Duplicate: Why is this C code causing a segmentation fault? char* string =
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Possible Duplicate: Matrix Arithmetic using Vectors in C++ causing segmentation faults I created this
Possible Duplicate: Working of fork() in linux gcc Why does this code print two
Possible Duplicate: How to get nth jQuery element I have this simple code: <ul>
Possible Duplicate: How to convert this code to use string I have a function
Possible Duplicate: Difference between WIN32 and other c string I got this code inside
Possible Duplicate: Find url from string with php I found this code: but 2
Possible Duplicate: Why is this code invalid in C#? Conditional operator cannot cast implicitly?
Possible Duplicate: How to compare Dates in C# This code of mine: public static

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.