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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:23:00+00:00 2026-06-18T00:23:00+00:00

First hallo to everyone, first post here. Today I found myself wondering if this

  • 0

First hallo to everyone, first post here.

Today I found myself wondering if this code is correct, it was written time ago by a friend for a microcontroller and I have no chance to ask him anymore.

The program works correctly, but I can’t really decide if it works by luck or not (don’t belive in luck with programming).
I don’t even know if the title of this post is correct, sorry if it could be misleading.
The code:

char  *textStatusErrorMessage( unsigned int codeStatus )
{
    switch ( codeStatus ) {

    case STATUS_1:
        return ( (char  *) " Status: 1 " );
        break;     
    case STATUS_2:
        return ( (char  *) " Status: 2 " );
        break;      
    default:
        sprintf( tmpBuf, " UNKNOWN STATUS   %03d ", codeStatus );
        return ( (char  *) tmpBuf ); //tmpBuf is global
        break;
    }
}

more precisely this syntax is kind of obscure to me.

return ( (char  *) " Status: 1 " );

It returns a char * of what ? where is the “Status: 1 ” string saved ? heap/stack???

As it’s implemented the string is in the scope of the function and I’m assuming that after leaving the function with the return statement I have no control of what is gonna be written in the pointer returned by the function itself.

From the way I see it I would have had a global array with the different possible string-options and returned a pointer to the correct one selected by the CASE. So I know the pointer I return is to a well defined memory area.

So is this code wrong or not? Which is the most correct solution?

Thanks
DAN

  • 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-18T00:23:01+00:00Added an answer on June 18, 2026 at 12:23 am

    In theory, this code is valid; string literals (e.g. " Status: 1 ") have a type of char[], and a lifetime equal to that of the whole program.

    However, there are a few issues:

    1. In the string literal cases, if anything tries to modify the returned string, then undefined behaviour occurs. Really, this function should return a const char * instead.

    2. It’s not clear where tmpBuf is declared, nor whether it points to sufficient memory to hold the data being written to it.

    3. There is only one tmpBuf, so every time this function is called, tmpBuf will be overwritten. This is likely to cause hard-to-track bugs.

    4. The casts are unnecessary.

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

Sidebar

Related Questions

Hello everyone (this is my first post) I have some simple AD code that
Hello everyone this is my first post on stack overflow.com I am trying to
This is my first post on stackoverflow, hello everyone! My first venture into jQuery
This is my first post, so first hallo all. I have a little problem
enter code here hello, everyone, can somebody explain what is my problem in this
First question here so hello everyone. The requirement I'm working on is a small
Hello everyone I am a newbie to php and this is my first ever
Hello everyone my problem is very simple. Using entity framework code first I want
Hello this is may first question and I have found so far the following
Hello everyone i need to convert this code to a function with 2 parameters

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.