Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda