1

I'm trying to make a "Truco" game and it's in the beginning but I don't have much experience with the C language.

I'm trying to make a function that receives an array of char (string) and returns another array of char.

I receive the following error when I compile my code:

276 [Warning] passing arg 1 of `retornaMaior' from incompatible pointer type

Here's relevant part of the code:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>

// const

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

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;
                }
            }
        }
    }
    char *carta = "YY";
    carta[0] = cartas_jogo[posMaior];
    a++;
    carta[1] = cartas[posA];
    return carta;
}

int main()
{
    char carta_jogada[3];
    printf("\n\nDigite uma de suas cartas para jogar: ");
    scanf("%s", &carta_jogada);

    // conferir length carta jogada
    // conferir se tem carta jogada
    char mesa_jog1[] = {'Y','Y','\0'};
    mesa_jog1[0] = carta_jogada[0];
    mesa_jog1[1] = carta_jogada[1];

    char jog2_carta1[] = {'Q','C','\0'};

    char cartas[7];
    cartas[0] = jog2_carta1[0];
    cartas[1] = jog2_carta1[1];
    cartas[2] = jog2_carta2[0];
    cartas[3] = jog2_carta2[1];
    cartas[4] = jog2_carta3[0];
    cartas[5] = jog2_carta3[1];
    cartas[6] = '\0';

    printf("%s", retornaMaior(&cartas,mesa_jog1[0]));
    getch();
}

This is a really short part of the code but I think that's enough for you guys help me out.

3
  • Loose the & in the call to retornaMaior(&cartas,mesa_jog1[0]) It should be retornaMaior(cartas,mesa_jog1[0]) Commented May 9, 2014 at 16:38
  • When I do it the compiler shows no errors but it stops to work when I enter a value... Commented May 9, 2014 at 16:40
  • note that cartsas where there is heap memory assigned in main is NOT the same as char *carta='YY'; and the several lines following. You are assigning to a string literal then trying to return that. There is your bug. Commented May 9, 2014 at 16:46

1 Answer 1

2

A lot of problems:

demo.c:68:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[3]’ [-Wformat]
demo.c:81:17: error: ‘jog2_carta2’ undeclared (first use in this function)
demo.c:81:17: note: each undeclared identifier is reported only once for each function it appears in
demo.c:83:17: error: ‘jog2_carta3’ undeclared (first use in this function)
demo.c:87:5: warning: passing argument 1 of ‘retornaMaior’ from incompatible pointer type [enabled by default]
demo.c:26:7: note: expected ‘char *’ but argument is of type ‘char (*)[7]’

And more ...

char *carta = "YY";
carta[0] = cartas_jogo[posMaior];  /* Error: carta is a string literal (read only) */
a++;
carta[1] = cartas[posA]; /* Error: carta is a string literal (read only) */
return carta;

Finally:

printf("%s", retornaMaior(&cartas,mesa_jog1[0]));

should be

printf("%s", retornaMaior(cartas,mesa_jog1[0]));

because cartas is (decays into) a pointer

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

2 Comments

Ok, but how can I solve this kind of problems like expected ‘char ’ but argument is of type ‘char ()[7]’
You solve this using retornaMaior(cartas,... note cartas without &

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.