Daniel Chaparro
Daniel Chaparro

Reputation: 13

How to assign values in a dynamic structure parameters

I have the following structure:

typedef struct {
  char nom[30], prenom[20];
  int age;
} ITEM;

//and I have a function where I am entering the info as follow:

void Lire(ITEM **items, int *nb_items)
{
    int i,j = 0, novalida = 0;
    ITEM *temp;
    printf("*** Entrer les donnees et taper . pour terminer\n\n");

    for (i = 0; TRUE; i++) {

        temp = (ITEM *) realloc(*items, ((*nb_items + 1) * sizeof(ITEM)));
        if (temp == NULL)
        {
            printf("Il n'y a pas de memoire! \n");
            exit (0);
        }
        *items = temp;
        printf("> nom    : ");
        scanf("%s", **items[i].nom);

and I got the the error:

 request for member ‘nom’ in something not a structure or union
 scanf("%s", **items[i].nom);

Then I want to know if I should assign the values to dynamic structures the same way I go with normal structures.

Upvotes: 1

Views: 150

Answers (2)

Étienne
Étienne

Reputation: 4994

You need some parentheses, because the operator . has a higher precedence than the operator *, you are doing **(items[i].nom) whereas you want (*items[i]).nom.

The error is telling you that you are trying to use the operator . on items[i], which has type ITEM * (not a structure or union).

Your example should be:

scanf("%s", (*items[i]).nom);

Upvotes: 1

aoak
aoak

Reputation: 1013

You should use something like

scanf("%s", items[i]->nom);

You have a pointer to pointer of ITEM, so items[i] accesses a pointer to ITEM, then you use -> to access the fields of the structure.

Upvotes: 1

Related Questions