I have to make a blackjack in c using the Graphic Library of our school and i'm on the motor of the game. I tried to use struct for a card with value and the symbol, an array of 52 cards using calloc and i've made 2 functions :
The function should return 1 if there's 2 identical cards :
int TestPaire(Carte *Jeu[]){ // La Fonction renvoie 1 s'il y a une paire, 0 sinon
for (int i = 0; i < sizeof(*Jeu); i++)
{
for (int j = 0; j < sizeof(*Jeu); i++)
{
if (Jeu[i]->Chiffre == Jeu[j]->Chiffre && i != j)
{
return 1;
}
}
}
return 0;
}
The function who return the total of the cards's values :
int CompteCarte(Carte *Jeu[]){ // La Fonction renvoie la valeur totale des cartes
int compteur = 0;
for (int i = 0; i < sizeof(*Jeu); i++)
{
compteur += Jeu[i]->Chiffre;
}
return compteur;
}
and there's my main :
int main(int argc, char const *argv[])
{
Carte* Jeu = (Carte*)calloc(52, sizeof(Carte));
Jeu[0].Chiffre = 3;
Jeu[1].Chiffre = 1;
Jeu[2].Chiffre = 6;
int a = CompteCarte(&Jeu);
printf("%d\n", a);
int b = TestPaire(&Jeu);
printf("%d\n", b);
return 0;
}
typedef struct Carte {
int Chiffre;
char Couleur[10];
} Carte;
And when runnig it in a code visualizer, it add the first value to the variable "compteur", but not the 2nd and return : "Invalid read of size 4". And in WSL, I have a segmentation fault. It just returns 10 then a 0 in the console.
Any help and/or advice (specially on pointers and dynamic allocation) ?