【发布时间】:2018-08-09 08:05:30
【问题描述】:
上下文:我们在图书馆里。我们写了两个结构:Livre(英文书),titer(标题)、nombre_pages(页数)和statut(是否已经借用?)
Lecteur (the reader) (nom = name; prenom = firstname; nb_livres = 读者已经预订的书籍数量;以及 struct livres)
我正在尝试执行一个参数为: 1)具有不同阅读器的数组(结构讲师) 2)数组的大小(带指针,因为它会进化) 3) 必须删除数组的读卡器(结构讲师)。
这是我的功能:
#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;
}
【问题讨论】:
-
你尝试的时候发生了什么?通过调试器运行它时发生了什么?
-
我尝试在主要内容中写入以尝试该功能,但是,即使我没有收到错误或警告,也没有发生任何事情。我将在前一条消息中发布我的主要内容。
-
您发布的代码甚至无法编译。请edit您的问题并发布minimal reproducible example
-
当我复制它时,一些箭头是这样的: - > 而不是这样 -> ,这会出错。对不起!!我改变了它,它编译得很好!!
-
在下一行中,数组索引
i + 1超出范围:*plecteurs[i] = *plecteurs[i + 1];您可以使用调试器自己发现这一点。 BTW:你的程序的整体设计很差。
标签: c arrays pointers structure function-pointers