Home > OS >  Globally defined variables as function return
Globally defined variables as function return

Time:11-06

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;
        }
    };

CodePudding user response:

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];
   }

CodePudding user response:

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] .

CodePudding user response:

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.

CodePudding user response:

You can use global variables as return values of the functions, but I would not keep it as a global variable:

const char* dayOfWeekToChar(int day)
{
    static const char * DaysOfWeekNames[] = 
               {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    const char *result = NULL; 
    if(day >= 0 && day < 7) 
        result = DaysOfWeekNames[day];
    return result;
}

CodePudding user response:

I'd say such style is sub-optimal. You should really just use an array.

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

with an enum to access the values:

enum DaysOfTheWeek {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
};

And you can do for eg. daynames[WEDNESDAY] to get "Wed".

And if you want it to only done using the preprocessor:

#define DAY1 "SUN"
#define DAY2 "MON"
#define DAY3 "TUE"
#define DAY4 "WED"
#define DAY5 "THU"
#define DAY6 "FRI"
#define DAY7 "SAT"

#define GET_DAY(num) DAY##num

The first method is strongly recommended.

  •  Tags:  
  • c
  • Related