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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:45:46+00:00 2026-06-01T22:45:46+00:00

Update: for the getValue function I have no control, so is there anything I

  • 0

Update: for the getValue function I have no control, so is there anything I can do from my side?

I have a kind of dumb question about string and char * basic.

I’m using a function that returns a char * value,

const char *getValue(const char *key)
{
    //if key found, and valueString is a string
      return valueString.c_str();
    //else
      return NULL;
}

then I initialized a string to hold the return value,

std::string value = getValue(key);

problem is, whenever the value is not found, which means the function returns NULL, my assignment line will run into an exception. But when there is a legal return value, everything is working fine.

I’m wondering
1. Is this usage totally wrong? means I should never mix char * with string?
2. If not, then when there is a legal pointer returned, does my string automatically make a copy and store it?
3. What is the best way to do this?

Thanks.

  • 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-01T22:45:47+00:00Added an answer on June 1, 2026 at 10:45 pm

    Since you have no control over what the getValue() function does, you need to check the return value for NULL before assigning it to the std::string.

    std::string value;  // value is an empty string
    const char *retVal = NULL;
    
    if( ( retVal = getValue(key) ) != NULL ) {
      value.assign( retVal );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a NumericUpDown control and want to update its contents from the registry.
Update: This does work, I was being stupid :( i have the following extension
********Update to Question********** If the tableview does not affect the model: Are the index
I'm using the Telerik RadComboBox control. I have two related combos and when the
How can i pass a class as a parameter in my function So far
I have a function that launches a couple of threads (it's a test function)
I am using Ember.js to create a table from a json feed. I have
I am writing a script to pull info/update a MsSQL server and i can
I have a XAML view with a list box: <control:ListBoxScroll ItemSource={Binding Path=FooCollection} SelectedItem={Binding SelectedFoo,
Could you help me: I have three table :there are :users(userid,uername,password,groupid),group(groupid,groupname),menus(menuid,menuname),permissions(menuid,gorupid): I would like

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.