Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Globally defined variables as function return

I have function that returns pointer to day of week name when pass day of week number. I have feeling I do something illegal by using globally defined strings as function return. Is such style acceptable?

const char* const SUN_NAME = "Sun";
const char* const MON_NAME = "Mon";
const char* const TUE_NAME = "Tue";
const char* const WED_NAME = "Wed";
const char* const THU_NAME = "Thu";
const char* const FRI_NAME = "Fri";
const char* const SAT_NAME = "Sat";


    char* dayOfWeekToChar(int day)
    {
        switch (day)
        {
            
        case SUN_NR :
            return SUN_NAME;
            break;
        case MON_NR :
            return MON_NAME;
            break;
        case TUE_NR :
            return TUE_NAME;
            break;
        case WED_NR :
            return WED_NAME;
            break;
        case THU_NR :
            return THU_NAME;
            break;
        case FRI_NR :
            return FRI_NAME;
            break;
        case SAT_NR :
            return SAT_NAME;
            break;
            
        default:
            break;
        }
    };
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

To elaborate on what Eugene Sh said:

  1. It is legal and acceptable.

  2. I'd put them in array though:

EXAMPLE:

   const char * DaysOfWeekNames[] = {
     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
   };

   enum DaysOfWeek {
     SUN_NR, MON_NR,TUE_NR, WED_NR, THU_NR, FRI_NR, SAT_NR
   };

   const char * dayOfWeekToChar(enum DaysOfWeek day)
   {
     return DaysOfWeekNames[day];
   }
over 4 years ago · Santiago Trujillo Report

0

What you do is OK, but it should be const char* dayOfWeekToChar(int day), because you return a pointer to a const char* and not a pointer to char*.

You could do this which is more or less the same as you do:

const char* dayOfWeekToChar(int day)
{
  static const char* days[] =
  {
    "Sun",
    "Mon",
    ...
  };

  return days[day];
}

However you should probably add some checks to make sure day is within the range [0..6] .

over 4 years ago · Santiago Trujillo Report

0

This is an acceptable way of doing things. strerror is an example of this concept that is used in the C library (strerror translates values of errno to useable string descriptions of the error that occurred).

However, I would recommend one of two things. Either make it very clear in your documentation that the return value of this function IS NOT TO BE MODIFIED BY THE PROGRAMMER, or you can dynamically allocate a new string so that the calling function can safely modify it.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!