0

I have some structs as following:

typedef struct {
    char debutAge[15];
    char finAge[15];
} Age;

typedef struct {
    char type[15];
    char composants[50];
    Age utilisation;
} Categorie;

typedef struct {
    int code;
    char nom[30];
    float prix;
    Categorie med;
    int quantitie;
} Medicament;

#define MAX 100
typedef Medicament Pharmacie[MAX];
Pharmacie P;
int nb=0; 

In my main function, I'm trying to add a new element to the Pharmacie P array, this is what I tried:

void main() 
{  
    Medicament m = Saisir(); 
    ajouterMedicament(P, nb, m);
    afficherMedicaments();
}

int ajouterMedicament(Pharmacie *ph, int *nb, Medicament m) {
    int i;

    for (i = 0; i < *nb; i++) {
        if (m.code == ph[i].code) {
            ph[i].prix = m.prix;
            ph[i].quantitie = m.quantitie;
        }
        return 1;
    }
    
    if (*nb < MAX) {
        ph[*nb] = m;
        *nb += 1;
        return 1;
    }
    return 0;
}

But I'm getting an error on this line: mif (m.code == ph[i].code) { :

expression must have struct or union type

How can I solve this ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

1 Answers1

3

You don't need to declare the first argument as Pharmacie *. Pharmacie is a typedef for an array, so you don't need to add *.

int ajouterMedicament(Pharmacie ph, int *nb, Medicament m) {

And in the call, you need to pass a pointer to nb so it can be updated:

ajouterMedicament(P, &nb, m);

In general it gets confusing when you use typedef for pointer types, and you ran into that. I recommend not doing that. See Is it a good idea to typedef pointers?

Barmar
  • 741,623
  • 53
  • 500
  • 612