I am a begginer programmer trying to do a project for a class on college that requires the use of dynamic strutures. My problem: I use realloc to increase the size of my struture and it works the first time, but not on the second. the auxiliary pointer wich I use to recieve the new increased struture seems to get the value NULL on the second time.
Here's my code so far:
funtion where I try to increase structure:
TList *addToQueue (TList *plist, int *counter, char departure)
{
TList *auxiliary;
auxiliary = NULL;
auxiliary = realloc(plist,(*counter+1)*sizeof(TList));
if (auxiliary == NULL)
{
printf("Insufficient Memory");
cleanstdin();
}
else
{
plist = auxiliary;
plist[*counter].departure = departure;
do{
printf("\nPoint of Arrival: ");
plist[*counter].arrival = checkMenuOption(OPTIONSLOC);
}while(plist[*counter].arrival == '\0');
printf("\nNumber of passengers:");
plist[*counter].numberOfPassengers = readIntenger(MIN_SEATS, MAX_SEATS);
(*counter) ++;
printf("\nTicket Purchased successfully");
cleanstdin();
}
return plist;
}
How I'ts declared above main:
TList *addToQueue (TList *plist, int *counter, char departure);
How I call in main:
char departure;
departure = sellTicketsMenu ();
switch (departure)
{
case 'A':
{
ListA = addToQueue (ListA, &counterA, departure);
cleanstdin();
}
break;
case 'B':
{
ListB = addToQueue (ListB, &counterB, departure);
cleanstdin();
}
break;
case 'C':
{
ListC = addToQueue (ListC, &counterC, departure);
cleanstdin();
}
break;
}
Other notes: readintenger - basic funtion to read intenger from keyboard cleanstdin - basic clear standarin function checkmenuoption - funtion to check the option from menus counters - counters to help with the index of the struture
If you need anything else to understand or to help me please do let me know. Thanks in advance.