Hello everyone. So in my programming we are in the structure subject, so the teacher gave us some exercise to practice, i'm having some troubles with one of the exercise which i need to make a list of things, print it and then choose with a menu if the user wants to add another item in the last position or delete the last item from the list.
I already did the first part, to print and capture the items (i'm working with 3 elements at the moment) but i'm having a lot of problems when trying to add and delete. One thing the teacher told us is that we need to use the realloc function. I already saw some example that are similar of what i'm doing but the thing is that it's so different of what i'm doing or it doesn't use realloc :/.
How i can apply that function in this situation?
This is the code i'm working on from the function, at the moment i haven't made the delete function (sorry if some of the stuff in the code is in spanish i made some comments to show more easy what's going on):
void agregaRegistro(struct automovil ***carro){ //Add registry in the last place
char marca[50]; //Brand of the car
char modelo[50]; //Model of the car
int annio; //Year of the car
fflush(stdin);
printf("Ingrese la marca del automovil: "); //Here the user capture the info of the last registry//
fgets(marca,sizeof(marca),stdin);
fflush(stdin);
printf("Ingrese el modelo del automovil: ");
fgets(modelo,sizeof(modelo),stdin);
fflush(stdin);
printf("Por ultimo, ingrese el año del automovil: ");
scanf("%d",&annio);
fflush(stdin);
(carro)[3]->marca=malloc(strlen(marca)sizeof(char)); //Here's where all the memory allocation for the registry goes, but i don't know how to use realloc in this//
(carro)[3]->modelo=malloc(strlen(modelo)sizeof(char));
strcpy((*carro)[3]->marca,marca); //Copian los datos
strcpy((*carro)[3]->modelo,modelo);
(*carro)[3]->annio=annio;
(*carro)[3]->siguiente=NULL;
for(int i=0;i<4;i++){ //Here i print all the 4 items(the first 3 ones and the one i already added//
printf("Los datos del automovil son: Marca: %s, Modelo %s del año %d",(carro)[i]->marca,(carro)[i]->modelo,(*carro)[i]->annio);
}
}
EDIT: I figured out how to make those functions for that exercise.