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

  • Home
  • SEARCH
  • 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 6024761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:12:09+00:00 2026-05-23T04:12:09+00:00

What i am trying to accomplish here is taking an cString and replacing a

  • 0

What i am trying to accomplish here is taking an cString and replacing a certain character with another which place i find via using the strchr() function.

what i can’t figure out is how you can replace the character all my attempts (below commented out) all produce either an unedited string or crash the program. i believe i am going in the right direction with replacing the character (take the starting address of char *c and add n (the number of bytes forward the character i want to replace is) and then write to that new address.), but i can’t seem to get it to function correctly.

any help is appreciated.

int main()
{
    char *c, *sch;
    int n;

    c = "this is a test\n";

    sch = strchr(c, 'a');

    if(sch != NULL)
    {
        n = sch-c+1;

        printf("%d\n", (int)sch);
        printf("%d\n\n", (int)c);

        printf("'a' found at: %d", n);
    }

    /////////////////////
    //sch = &c;
    //*(sch + n) = 'z';
    /////////////////////
    //*(c + n) = 'z';
    /////////////////////
    //c[n] = 'z';
    /////////////////////

    printf("\n\n%s", c);

    getchar();
    return 0;
}
  • 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-23T04:12:10+00:00Added an answer on May 23, 2026 at 4:12 am

    Edit: first of all, c should be an array and not a pointer to a string literal, since string literals are stored in read-only memory, and trying to modify them will usually result in a crash.

    So, first of all,

    char c[]= "this is a test\n"
    

    This initializes a modifiable string to that value, that you can edit without problems.

    In general, you shouldn’t directly assign string literals to char *, because you can incur in this kind of problems; instead, assign them only to const char *, that way any modification attempt will result in a compilation error.


    Then, strchr already returns a pointer to the matching character (as stated in the documentation), you can simply change it directly, no pointer arithmetic is involved:

    *sch = 'z';
    

    Still, if you want to experiment with pointer arithmetic, there’s an error in the definition for n:

    n = sch-c+1;
    

    If then you use it as an index of the string, you should remove that 1, because arrays (and strings) are zero-based. In other words: if you assign to n that value, the following code:

    *(c + n) = 'z';
    

    (which is equivalent to

    c[n] = 'z';
    

    )

    Will mean

    *(c + sch - c + 1) 
    

    that is,

    *(sch+1)
    

    i.e. the character following the one found by strchr. Removing the +1 will do the trick (although it’s just a convoluted way to simply say *sch='z').

    The other try is wrong because in your code &c will yield a char **, i.e. a pointer to a pointer to char, which is not what you want.

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

Sidebar

Related Questions

Here is what I am trying to accomplish by using NServiceBus. I have a
Ok here is an example what I'm trying to accomplish: <div class=testClass1 {place xsl:value-of
What I am trying to accomplish here is to separate a firstname, lastname combo
I'm trying to accomplish what is described here: http://sqldev.wordpress.com/2008/05/06/insert-into-temporary-table-from-stored-procedure/ The article says (with support
I'm trying to accomplish what should be a very simple task using the ListView
I'm trying to create a camera activity for taking photos to be OCR'd. Here's
What I am trying to accomplish here is a dictionary with linked list. There
Hi here's what I'm trying to accomplish. I have a div with ID=columns, in
So ideally here is what I am trying to accomplish (keep in mind I
Here is what I'm trying to accomplish. I have an object coming back from

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.