Context: We are in a library. We wrote two structures: Livre (book in english) with titre (title), nombre_pages (number of pages) and statut (borrowed already or not? )
Lecteur (the reader) (nom = name; prenom = firstname; nb_livres = number of books the reader has booked already; and a struct livres)
I'm trying to do a function in which the parameters are: 1) Array with different readers (structure Lecteur) 2) The size of the array (with a pointer because it will evolve) 3) The reader (structure Lecteur) that has to be delete of the array.
Here is my function:
#include <stdio.h>
struct Livre {
char titre[100];
int nombre_pages;
int statut; // Book already borrowed = 1, Available = 0
};
struct Lecteur {
char nom[100];
char prenom[100];
int nb_livres; // le nombre de livres dans le tableau "livres"
struct Livre* livres[100]; // livres deja empruntes (eventuellement rendus)
};
void desabonnement(struct Lecteur * plecteurs[], int * nombre_lecteurs,
struct Lecteur * lect) {
struct Lecteur empty = { // Cette variable me permettra de transformer la valeur qui m'intérésse pas
0
};
int i = 0;
int j = 0;
while ((plecteurs[i]->nom != lect->nom) &&
(plecteurs[i]->prenom != lect->prenom)) {
i++;
}
while (j < plecteurs[i]->nb_livres) {
plecteurs[i]->livres[j]->statut = 0;
j++;
}
while (i < * nombre_lecteurs) {
*plecteurs[i] = *plecteurs[i + 1];
i++;
}
*plecteurs[i] = empty;
}
int main() {
struct Livre l1 = { "boom" , 50 , 1 };
struct Livre l2 = { "bim" , 50 , 1 };
struct Livre l3 = { "chaud" , 50 , 0 };
struct Livre l4 = { "tcho" , 50 , 1 };
struct Livre l5 = { "braa" , 50 , 1 };
struct Livre *p1 = & l1;
struct Livre *p2 = & l2;
struct Livre *p3 = & l3;
struct Livre *p4 = & l4;
struct Livre *p5 = & l5;
struct Lecteur le1 = { "Boso" , "Nen" , 2 , {&l1, &l2} };;
struct Lecteur le2 = { "Jogar" , "Elo" , 1 , {&l3} };;
struct Lecteur le3 = { "marche" , "silteplait" , 2 , {&l4, &l5} };;
struct Lecteur *tableau_test[3] = {&le1, &le2, &le3};
int le_nombre = 3;
desabonnement(tableau_test, &le_nombre, &le3);
printf(" %d ", tableau_test[0]->nb_livres);
return 0;
}
i + 1goes out of bounds:*plecteurs[i] = *plecteurs[i + 1];You could have found out this yourself by using your debugger. BTW: the overall design of your program is very poor.