I have created a dynamic list:
The struct is:
typedef struct{
char *id;
char *name;
char *surname;
int age;
char gender;
char *username;
char *password;
char *description;
char *hobbies;
}User;
And after I create User **headMan I add users via:
void newMan(User **headMan, int *size, char *id, char *name, char *surname,
int age, char gender, char *username,
char *password, char *description, char *hobbies){
if(*size == 0){
*headMan = (User*)malloc(sizeof(User));
if(*headMan == NULL){
printf("Allocation of (*headMan) failed\n");
exit(1);
}
(*headMan) -> id = (char*)malloc(ID_LENGTH*sizeof(char));
if ((*headMan) -> id == NULL){
printf("Allocation of (*headMan) -> id failed\n");
exit(1);
}
strcpy((*headMan) -> id,id);
(*headMan) -> name = (char*)malloc(NAME_LENGTH*sizeof(char));
if ((*headMan) -> name == NULL){
printf("Allocation of (*headMan) -> name failed\n");
exit(1);
}
strcpy((*headMan) -> name,name);
(*headMan) -> surname = (char*)malloc(NAME_LENGTH*sizeof(char));
if ((*headMan) -> surname == NULL){
printf("Allocation of (*headMan) -> surname failed\n");
exit(1);
}
strcpy((*headMan) -> surname, surname);
(*headMan) -> age = age;
(*headMan) -> gender = gender;
(*headMan) -> username = (char*)malloc(MAX*sizeof(char));
if ((*headMan) -> username == NULL){
printf("Allocation of (*headMan) -> username failed\n");
exit(1);
}
strcpy((*headMan) -> username, username);
(*headMan) -> password = (char*)malloc(NAME_LENGTH*sizeof(char));
if ((*headMan) -> password == NULL){
printf("Allocation of (*headMan) -> password failed\n");
exit(1);
}
strcpy((*headMan) -> password, password);
(*headMan) -> description = (char*)malloc(DESCRIPTION*sizeof(char));
if ((*headMan) -> description == NULL){
printf("Allocation of (*headMan) -> description failed\n");
exit(1);
}
strcpy((*headMan) -> description, description);
(*headMan) -> hobbies = (char*)malloc(NAME_LENGTH*sizeof(char));
if ((*headMan) -> hobbies == NULL){
printf("Allocation of (*headMan) -> hobbies failed\n");
exit(1);
}
strcpy((*headMan) -> hobbies, hobbies);
(*size)++;
}
else{
headMan[*size] = (User*)malloc(sizeof(User));
if(headMan[*size] == NULL){
printf("Allocation of headMan[*size] failed\n");
exit(1);
}
headMan[*size] -> id = (char*)malloc(ID_LENGTH*sizeof(char));
if (headMan[*size] -> id == NULL){
printf("Allocation of headMan[*size] -> id failed\n");
exit(1);
}
strcpy(headMan[*size] -> id,id);
headMan[*size] -> name = (char*)malloc(NAME_LENGTH*sizeof(char));
if (headMan[*size] -> name == NULL){
printf("Allocation of headMan[*size] -> name failed\n");
exit(1);
}
strcpy(headMan[*size] -> name,name);
headMan[*size] -> surname = (char*)malloc(NAME_LENGTH*sizeof(char));
if (headMan[*size] -> surname == NULL){
printf("Allocation of headMan[*size] -> surname failed\n");
exit(1);
}
strcpy(headMan[*size] -> surname, surname);
headMan[*size] -> age = age;
headMan[*size] -> gender = gender;
headMan[*size] -> username = (char*)malloc(MAX*sizeof(char));
if (headMan[*size] -> username == NULL){
printf("Allocation of headMan[*size] -> username failed\n");
exit(1);
}
strcpy(headMan[*size] -> username, username);
headMan[*size] -> password = (char*)malloc(NAME_LENGTH*sizeof(char));
if (headMan[*size] -> password == NULL){
printf("Allocation of headMan[*size] -> password failed\n");
exit(1);
}
strcpy(headMan[*size] -> password, password);
headMan[*size] -> description = (char*)malloc(DESCRIPTION*sizeof(char));
if (headMan[*size] -> description == NULL){
printf("Allocation of headMan[*size] -> description failed\n");
exit(1);
}
strcpy(headMan[*size] -> description, description);
headMan[*size] -> hobbies = (char*)malloc(NAME_LENGTH*sizeof(char));
if (headMan[*size] -> hobbies == NULL){
printf("Allocation of headMan[*size] -> hobbies failed\n");
exit(1);
}
strcpy(headMan[*size] -> hobbies, hobbies);
(*size)++;
}
}
Now I move on the user list with an index for example headMan[i] now when I try to remove a user I use:
void removeMan(User** head, int* numberOfMen,char* existUser){
if ((strcmp(head[1] ->username,existUser) == 0)){
freeUser(head[1]);
free(head[1]);
head = (User**)realloc(head, (*numberOfMen-1)*sizeof(User));
}
printListMen(head,numberOfMen);
}
When freeUser frees all the fields of the struct, now I can not understand how the realloc works.
If I have a list of 5 users and I remove the one in the 3rd place, using realloc will move the resize the list to 4? who will be listed in 3rd place?