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.
&in the call toretornaMaior(&cartas,mesa_jog1[0])It should beretornaMaior(cartas,mesa_jog1[0])cartsaswhere there is heap memory assigned in main is NOT the same aschar *carta='YY';and the several lines following. You are assigning to a string literal then trying to return that. There is your bug.