I am having this issue when trying to build my dynamic array, I think that there is mystake on realloc function or something, would appreciate if you can give me a hand on it. I am able to enter the names the first time but the issue start when Im doing it the second time
typedef struct {
char nom[30], prenom[20];
int age;
} ITEM;
void Lire(ITEM **items, int *nb_items)
{
int i = 0;
printf("* Entrer les donnees et taper . pour terminer\n\n");
for (i = 0; TRUE; i++)
{
ITEM *temp = (ITEM *) realloc(*items, ((i + 1) * sizeof(ITEM)));
if (temp == NULL)
{
free(*items);
printf("Il n'y a pas de memoire! \n");
exit (0);
}
*items = temp;
printf("> nom : ");
scanf("%s", (*items[i]).nom);
if ((*items[i]).nom[0] == '.')
break;
printf("> prenom : ");
scanf("%s", (*items[i]).prenom);
}
}
int main(int argc, char *argv[])
{
ITEM *items;
int nb_items=0;
items = (ITEM *) malloc(sizeof(ITEM));
if(items == 0)
{
printf("Il n'y a pas de memoire! \n");
exit (0);
}
Lire(&items, &nb_items);
free (items);
exit(0);
}
C++. Why are you not usingstd::vector?scanf("%29s", (*items[i]).nom);realloccan work just fine withoutmalloc/free. If the initial valueNULL,reallocworks asmalloc. If the requested size is 0,reallocworks asfree.