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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:35:49+00:00 2026-05-11T09:35:49+00:00

After reading the faq’s and everything else I can find, I’m still confused. If

  • 0

After reading the faq’s and everything else I can find, I’m still confused. If I have a char pointer that is initialised in this fashion:

char *s = 'Hello world!'

The string is in read-only memory and I cannot change it like this:

*s = 'W'; 

to make ‘Wello world!’. This I understand, but I can’t, for the life of me, understand how to make it NOT read-only. Do I have to use an array instead of a pointer? Like here?

This is my code:

char *s = str; char *e = s; while (*e != '\0') e++; e--; char *temp; //Swop the string around while (s <= e) {     *temp = *s;     *s = *e;     *e = *temp;     e--;     s++; } 

The error message is just a segmentation fault. Apologies in advance if this is a really stupid question.

Thanks so much for all the help. After taking all your advice, I get this:

void something(char * str) {     char *store = str;     char *s = new char[strlen(str) + 1]; //Allocate memory. Nice one.     strcpy(s, str);     char *e = new char[strlen(str) + 1];     strcpy(e, str);     while (*e != '\0')         e++;     e--;     char temp; //no longer a pointer     while (s <= e) {         cout << *e;         temp = *s;         *s = *e;         *e = temp;         e--;         s++;      }     delete [] e;     delete [] s;         } 

however, the deletes at the end of the function seem to be causing their own segmentation faults. Why?

For interest’s sake: The faults were due to accessing the e and s pointers after they were incremented. A much simpler solution followed from that:

void something(char * str) {     char *s = new char[strlen(str) + 1];     strcpy(s, str);     char temp;     int j = strlen(str) - 1;     for (int i = 0; i <= strlen(str)/2; i++) {         cout << s << endl;         temp = s[i];         s[i] = s[j];         s[j] = temp;         j--;     }     delete [] s; } 
  • 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. 2026-05-11T09:35:49+00:00Added an answer on May 11, 2026 at 9:35 am

    The easiest way to modify it is to create an array for your storage, and then copy the string into it.

    For example:

    char buf[128]; const char *src = 'Hello World'; strncpy(buf, src, 127); // one less - we always 0-terminate buf[127] = '\0';  // you can now modify buf buf[0] = 'W'; 

    The reason your code doesn’t work is that you haven’t allocated any memory for the copy of the string – you’ve just made a second pointer to the same read-only memory. (And then tried to copy it? I’m not quite sure what the rest of the code is doing.) You need to get some non-read-only memory somewhere, and it’s much easier to use the standard library to copy it into that new memory, rather than writing the loop yourself.

    In the case when you don’t know the length of the string beforehand, you can also use malloc (or, even better, do what drschnz’s answer says and use new char[]):

    const char *src = 'Hello world'; char *buf = malloc(strlen(src) + 1);   // or = new char[strlen(src) + 1]; strcpy(buf, src); // you can now modify buf // later, you need to free it free(buf);                             // or delete [] buf; 

    Also, if you’re using C++, you can just use a std::string:

    std::string myString('Hello world'); myString[0] = 'W'; 

    Hope that helps.

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

Sidebar

Related Questions

after reading the cookbook and various Q&A here I am still a bit confused
After reading the answer to this question , I learned that SFINAE can be
After reading RailsGuides , some answers here and other docs I am still confused
I am coming back from after reading this c-faq question I am totaly confused
After reading several questions about .gitignore I have found no one that could answer
After reading through various tutorials [1] I still can't get my VirtualHost settings to
After reading countless guides, tutorials and intros, I still can't get my app onto
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is
After reading MSDN-XAML Namespaces and MSDN-Understanding XAML Namespaces , I still do not understand
After reading some posts like this one: Choose File Dialog It appears that Android

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.