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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:48:38+00:00 2026-05-26T00:48:38+00:00

How would I get my replace_char function to work properly? The way that I

  • 0

How would I get my replace_char function to work properly?

The way that I am trying it in the function below returns segmentation faults using gcc in Ubuntu.

I have tried other ways, but each time I try to change the value, I get a fault.

int main (void)
{  
  char* string = "Hello World!";

  printf ("%s\n", string);
  replace_char(string, 10, 'a');
  printf ("%s\n", string);
}

void replace_char(char str[], int n, char c)
{
  str[n] = c;
}
  • 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-26T00:48:38+00:00Added an answer on May 26, 2026 at 12:48 am

    Edit

    To get the ‘suggestion’ of editing string in place, you can edit the pointer inplace:

    void replace_char(char*& str, int n, char c)
    {
      str = strdup(str);
      str[n] = c;
    }
    
    int main()
    {
        char* string = "Hello World!";
        string = replace_char(string, 10, 'a');
    
        // ...
        free(string);
    }
    

    Note you now have to remember to call free on the string after calling this. I suggest, instead, that you do what I suggested before: wrap the literal in strdup if required. That way you don
    ‘t incur the cost of allocating a copy all the time (just when necessary).


    The problem is that “Hello World’ is a const literal char array.

    const char* conststr = "Hello World!";
    char * string = strdup(conststr);
    

    i assume the problem will be gone

    Explanation:
    Compilers can allocate string literals in (readonly) data segment.
    The conversion to a char* (as opposed to const char*) is actually not valid. If you use use e.g.

    gcc -Wall test.c
    

    you’d get a warning.

    Fun experiment:

    Observe here that (because it is Undefined Behaviour) compilers can do funny stuff in such cases:

    http://ideone.com/C39R6 shows that the program wouldn’t ‘crash’ but silently fail to modify the string literal unless the string was copied.

    YMMV. Use -Wall, use some kind of lint if you can, and do unit testing :){

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

Sidebar

Related Questions

Could someone supply some code that would get the xpath of a System.Xml.XmlNode instance?
I'm looking for a method or an event that would get fired when a
Can someone please shed some light on how I would get this to work:
The function below takes the argv[0] argument that contains the calling path of the
I would like to get the final SQL query, with parameter values bound (replaced)
How would get find an average from an array? If I have the array:
Following is how you would get the current solution directory from an add-in: _applicationObject
I was wondering if in Java I would get any odd behaviour if I
Just wanted to know if overriding UITabBarController would get my app rejected? Is it
In the past, when one made a JPopupMenu visible it's first item would get

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.