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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:07:23+00:00 2026-06-15T14:07:23+00:00

I have written this little weird piece of code. How is this possible that

  • 0

I have written this little weird piece of code. How is this possible that type changes between its 2 printf ??

Thanks in advance

int main()
{
    string label = string("faults_team_A_player_12");

    size_t f = label.find('_');

    const char *type = label.substr(0,f).c_str();
    const char team = label.at(f+sizeof("team_"));

    printf("type = %s\n",type);

    int n;
    size_t l = label.length()-label.find_last_of('_');

    int x = sscanf((char *)label.substr(label.find_last_of('_'),l).c_str(),"_%d",&n);
    printf("type = %s\n",type);
    printf("team = %c\n",team);
    printf("player = %d\n",n);

    return 0;
}

ouptut:

type = faults
type = _12
team = A
player = 12
  • 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-15T14:07:24+00:00Added an answer on June 15, 2026 at 2:07 pm

    type is a dangling pointer as it is initialised to the internal member of a temporary std::string instance:

    const char *type = label.substr(0,f).c_str();
    

    The std::string instance from which the result of c_str() is obtained is destructed immediately.

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

Sidebar

Related Questions

I have written this little piece of code: GKPeerPickerController *picker = [[GKPeerPickerController alloc] init];
I have written a little program that parses log files of anywhere between a
I have written this little script to find executables that are passed as arguments
have written this little class, which generates a UUID every time an object of
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written this code what it does is if user types postcode or
I have written this code outside all functions: int l, k; for (l =
I have two tables that look a little like this AGR_TERR_DEF_TERRS ID DEF_ID TERRITORY_ID
I have some code within one function that I want to separate into its

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.