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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:39:20+00:00 2026-05-15T03:39:20+00:00

I am trying to change the value of a static char * I define

  • 0

I am trying to change the value of a “static char *” I define at startup, I do it from inside a function, and when this function returns the var I am trying to re-set the value doesn’t retain it.

Example:

static char *X = "test_1";

void testFunc()
{
    char buf[256];
    // fill buf with stuff...
    X = buf;
}

How can I achieve this without using static for buf? Should I use another datatype? if so, which one?

  • 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-15T03:39:20+00:00Added an answer on May 15, 2026 at 3:39 am

    As James said, use std::string… except be aware that global construction and destruction order is undefined between translation units.

    So, if you still want to use char*, use strcpy (see man strcpy) and make sure buf gets NUL-terminated. strcpy will copy the buf into the destination X.

    char buf[256];
    // ...
    strcpy(X, buf);
    

    I should add that there are more reasons to use std::string. When using strcpy, you need to make sure that the destination buffer (X) has enough memory to receive the source buffer. In this case, 256 is much larger than strlen("test_1"), so you’ll have problems. There are ways around this reallocate X (like this X = new char[number_of_characters_needed]). Or initialize X to a char array of 256 instead of a char*.

    IIRC, strcpy to a static defined string literal (like char *X = “test_1”) is undefined behavior… the moral of the story is… It’s C++! Use std::string! 🙂

    (You said you were new to c++, so you may not have heard “undefined behavior” means the computer can punch you in the face… it usually means your program will crash)

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

Sidebar

Related Questions

I'm trying to execute this function when a select list's (LevelId0) value change but
I am trying to change the value of onclick of a td from a
I am trying to change a static variable inside a DLL, so when the
I'm trying to understand what's the best Cache-Control value to be set for static
I'm trying to change the value of a JComboBox, from an ActionListener, and having
I have a simple jsfiddle where i am trying to change value of each
I'm trying to change the value of an input field with Javascript. I tried
I'm trying to change the value of a dojo tree to display the correct
I am trying to change a specific value, which has no connection to it's
I am trying to programmatically change a value of a wxRadioButton in a way

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.