0

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);
}
12
  • You tagged this question as C++. Why are you not using std::vector? Commented Oct 31, 2014 at 17:35
  • 1
    You can´t use realloc alone. malloc and free are there to use too. Commented Oct 31, 2014 at 17:35
  • thanks, I am using them in the main this is just a function to fill my array, should I use malloc and free here as well? Commented Oct 31, 2014 at 17:38
  • 1
    Are you typing a name that is too long? You are not protecting your strings from overrun. Try scanf("%29s", (*items[i]).nom); Commented Oct 31, 2014 at 18:06
  • 1
    Actually realloc can work just fine without malloc/free. If the initial value NULL, realloc works as malloc. If the requested size is 0, realloc works as free. Commented Oct 31, 2014 at 18:12

3 Answers 3

2

You problem is in operator precedence: *items[i] evaluates to items[i][0] and you want items[0][i], which is:

(*items)[i]
answered Oct 31, 2014 at 18:07
Sign up to request clarification or add additional context in comments.

Comments

1

Array subscript access binds tighter than *. This causes *items[i] to be interpreted as *(items[i]), for example in this statement:

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

So items is accessed as if it would be an array of pointers to ITEM. In reality it is a pointer to an array of ITEM structs, and should be accessed like this:

scanf("%s", (*items)[i].nom);
answered Oct 31, 2014 at 18:10

Comments

0

to make life easier i would do

 *items = temp;
 ITEM *current = &temp[i];
 printf("> nom : ");
 scanf("%s", current->nom);
 ...etc

makes the code easier to read and simplifies the whole operator precedence isuuse

answered Oct 31, 2014 at 18:15

1 Comment

actually this issue was solved today with your help, the names are being inserted into the array, the isse is reallocating memory I think as I am getting this segmentation Fault(Core dumped) when I need to enter the second block of info

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.