0

I have a function called "retornaMaior" which returns a char array, and I want to assign this return to a new variable like:

char variable[] = retornaMaior();

Is there any way to do this? If there is, how can I do that?

I was thinking about declaring the "variable" as a global like "cartas_jogo" but I don't know if it's right.

Code below:

char cartas_jogo[] = "4567QJKA23";
char naipes_jogo[] = "ZCEO";
char carta_retorno[2];

int retornaIndice(char carta)
{
    int a = 0;
    int retorno = 0;
    for (a=0; a<strlen(cartas_jogo); a++)
    {
        if (carta==cartas_jogo[a])
        {
            retorno = a;
        }
    }
    return retorno;
}

char *retornaMaior(char *cartas, const char carta_comp)
{
    int posCartaComp = retornaIndice(carta_comp);
    int a = 0;
    int posMaior = 0;
    int tem = 0;
    int posA = 0;
    for (a=0; a<6; a+=2)
    {
        int posCarta = retornaIndice(cartas[a]);
        if (tem == 0)
        {
            if (posCarta>posCartaComp)
            {
                posMaior = posCarta;
                posA = a;
                tem = 1;
            }
        }
        else
        {
            if (posCarta<posMaior)
            {
                if (posCarta>posCartaComp)
                {
                    posMaior = posCarta;
                    posA = a;
                }
            }
        }
    }
    carta_retorno[0] = cartas_jogo[posMaior];
    posA++;
    carta_retorno[1] = cartas[posA];
    if (tem==0)
    {
       carta_retorno[0] = '0';
       carta_retorno[1] = '0';
    }
    return carta_retorno;
}
3
  • 1
    In C you cannot return arrays. You merely can return the pointer to the first element of an array. That's actually what you are doing in the retornaMaior function. Commented May 12, 2014 at 17:07
  • Variable carta_retorno is global, so there's nothing really that you need to do (besides removing the useless return carta_retorno statement). Commented May 12, 2014 at 17:36
  • You can create an array and pass it to the function, and have the function modify it. That way, you don't have to worry about returning anything at all. Commented May 12, 2014 at 17:44

1 Answer 1

3
char variable[] = retornaMaior();

Probably won't work. Instead:

char *variable = retornaMaior();

Which can still be addressed as an array:

printf("%c\n", variable[1]);

Within the 'retornaMaior()' function, you will need to put the value returned into memory that can be returned. Using the global variable 'carta_retorno' will probably work; but there is a better way.

Another option would be to allocate some 'heap' memory to the return value. To do this, move 'carta_retorno' from global storage, and move it inside the 'retornaMaior()' function:

char carta_retorno[2];              // ---

char *retornaMaior(char *cartas, const char carta_comp)
   {
   int posCartaComp = retornaIndice(carta_comp);
   int a = 0;
   int posMaior = 0;
   int tem = 0;
   int posA = 0;

   char *carta_retorno = malloc(2);   // +++

As indicated by 'staticx', it is important to ensure that malloc() succeeded:

   if(NULL == carta_retorno)
      ...

Now, the 'retornaMaior()' function will return the result in allocated memory (rather than using global storage).

Don't forget that the caller to 'cp=retornaMaior()' needs to call 'free(cp)' when the return value is no longer needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Always check the return value of malloc also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.