0

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) ?

DilDeath
  • 5
  • 3
  • 4
    Welcome to SO. This code `i < sizeof(*Jeu)` clearly does not what you might expect it to do. `sizeof *Jeu` is the size of the struct in bytes. It does not relate to the number of elements you allocated memory for. You cannot use it as index for `Jeu[j]`. You must pass the number of elements as another parameter to your function. – Gerhardh Jun 13 '23 at 15:00
  • 1
    Does this answer your question? [Why sizeof(param\_array) is the size of pointer?](https://stackoverflow.com/questions/11622146/why-sizeofparam-array-is-the-size-of-pointer) – Gerhardh Jun 13 '23 at 15:01
  • That duplicate is a tiny bit different as you use `*Jeu` instead of the pointer itself but the problem is the same. – Gerhardh Jun 13 '23 at 15:02
  • I just saw there is another error in your code. Your compiler should warn you that you pass an argument of incompatible type. Your function expects an array of pointers, not a pointer to an array. The correct parameter list would be `Carte Jeu[]` or `Carte *Jeu` which are identical. That doesn't solve the issue with calculating the size. – Gerhardh Jun 13 '23 at 15:05
  • @Gerhardh so to get the size of an array using an array i should use " sizeof(jeu)/sizeof(*Jeu) ? – DilDeath Jun 13 '23 at 15:31
  • No, as I wrote in my first comment, `sizeof` does not retrieve the size of the allocated memory. It will result in the size of a pointer. Read my comments carefully and also read the linked question again. Together with the answers, of course. – Gerhardh Jun 13 '23 at 17:06
  • 1
    @DilDeath In C, there is no way to retrieve the size of an array that was passed into a function. You have to manually record the length of the array when you create it, and explicitly provide that information to the function. – bta Jun 13 '23 at 17:42

1 Answers1

0

Passing a double pointer to a function can result in some cryptic errors and segmentation faults (like in your case) and is usually never mandatory (unless you're doing some truly cursed stuff). You already have Jeu as a Carte* - why not just pass that directly to CompteCarte and TestPaire? If you need the size of the array, you would have to pass that as a second parameter:

For example, for CompteCarte:

int CompteCarte(Carte* Jeu, int size) { // La Fonction renvoie la valeur totale des cartes
    int compteur = 0;
    for (int i = 0; i < size; i++)
    {
        compteur += Jeu[i].Chiffre;
    }
    
    return compteur;
}

and in main:

int a = CompteCarte(Jeu, 52 /* or whatever the size of Jeu is */);
azhen7
  • 371
  • 1
  • 5