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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:12:44+00:00 2026-06-09T18:12:44+00:00

I am trying to do some simple updating of a string in a function,

  • 0

I am trying to do some simple updating of a string in a function, I got the following sample to work:

void change(char* buffer) {
  buffer[0] = 'b';
}

void main() {
  char buffer[20] = "abc   def   ghi   j\0";
  printf("before: .%s., %p\n", buffer, buffer);
  change(buffer);
  printf("after:  .%s.\n", buffer);
}

But if I use char* for buffer instead of char[], I get an error in the function. So the following example doesn’t work:

void compact(char* buffer) {
  buffer[0] = 'b';
}

void main() {
  char* buffer="abc   def ghi\0";
  printf("before: .%s., %p\n", buffer, buffer);
  change(buffer);
  printf("after:  .%s.\n", buffer);
}

Any suggestions on what I am doing wrong? Thanks.

Don

  • 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-09T18:12:46+00:00Added an answer on June 9, 2026 at 6:12 pm

    If you use char*, your variable points to a string literal and invokes undefined behaviour.

    You can’t modify a string literal. When you declare a pointer like this

    char* buffer="abc   def ghi\0";
    

    buffer will point to read-only memory (probably, implementation defined). Regardless, you can’t change its contents. This syntax was kept for compatibility with C, but, IMO, this shouldn’t compile. Whenever you see something like that (char* pointing to a C-string), immediately translate it, inside your head, to

    const char* buffer="abc   def ghi\0";
    

    Put on your C++ glasses. If you don’t have them, get a pair (you’ll see all sorts of stuff, like this, returning locals by reference, forgetting to return, and many more).

    The other case:

    char buffer[] = "abc   def ghi\0";
    

    is fine because you actually create an array of the required size and populate it with that content. This is no longer a pointer to a string literal.

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

Sidebar

Related Questions

I'm trying to set a simple cron script to do some database updating, and
I'm trying to make work a self-updating functionality in an Android widget, something simple
I'm trying to do some automation scripts using git, simple things like updating submodules,
I'm trying to convert some simple .htaccess rewrites into web.config xml; all is well
I am trying to write some simple code which will read a text file
I'm trying to do some simple things in C and I'm confused. The program
I was trying to place some simple effects on an image using HBox using
I am trying to create some simple polygons in openGL3.3. I have 2 types
I'm trying to add some simple peer-to-peer connection functionality to an iOS library. Coding
I'm trying to implement some simple caching into my application and I'm having some

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.