Given an array of a struct with several elements, I would like to delete, say element 2, and want the elements 3,4,5.. to shift to 2,3,4.
In my code below, the add and delete functions work fine. I have tried with strcpy() function but it didn't work.
struct Books {
char title[20];
char author[20];
};
void add_book(struct Books book1[], int *counter){
fflush(stdin);
printf("Title: ");
gets(book1[*counter].title);
printf("Author: ");
gets(book1[*counter].author);
*counter++;
return;
}
void delete_book(struct Books book1[], int *counter){
int i = 0;
int delete = 0;
printf("What nr of book you want to delete: ");
scanf("%d", &delete);
book1[delete-1].title[0] = '\0';
book1[delete-1].author[0] = '\0';
*counter--;
/*
here I want to move elements down one step if I delete for example one
element in the middle
*/
return;
}
int main(){
struct Books book1[50];
int count = 0; //for keeping track of how many books in the register
add_book(book1, &count);
delete_book(book1, &count);
return 0;
}
memmoveis a very good function to move (overlapping) data around in memory.fflushand passing an input-only stream (likestdin) is explicitly mentioned as undefined behavior by the C specification. Some standard library implementations have added it as an extension, but please try to avoid it.scanfwrong.