Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda