简体   繁体   English

函数以指针为参数的数组

[英]function with array with pointer as parameter

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? ) 我们编写了两种结构:Livre(英语书籍),带有titre(标题),nombre_pages(页数)和statut(是否已借用?)

Lecteur (the reader) (nom = name; prenom = firstname; nb_livres = number of books the reader has booked already; and a struct livres) 讲者(读者)(nom =名称; prenom =名字; nb_livres =读者已经预订的书数;以及一个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. 我正在尝试执行一个函数,其中的参数为:1)具有不同读取器的数组(结构Lecteur)2)数组的大小(带有指针,因为它将不断演化)3)具有以下功能的读取器(结构Lecteur)删除数组。

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;
}

The problem is in line while (i < * nombre_lecteurs) of function void desabonnement(struct Lecteur * plecteurs[], int * nombre_lecteurs, struct Lecteur * lect) . 问题出在函数while (i < * nombre_lecteurs) void desabonnement(struct Lecteur * plecteurs[], int * nombre_lecteurs, struct Lecteur * lect) This should be while (i+1 < * nombre_lecteurs) . 这应该是while (i+1 < * nombre_lecteurs)

See complete working code here . 在此处查看完整的工作代码。

Note: In your actual code, You should decrease the le_nombre when deletion is complete to reflect the new size (I have done this in the corrected code here ). 注意:在您的实际代码中,应在删除完成后减小le_nombre以反映新的大小(我在此处更正代码中已完成此操作)。

Actually what is your question? 其实你的问题是什么?

Just some remarks: 请注意:

First I'd consider (plecteurs[i]->nom != lect->nom) a shorthand for strcmp(plecteurs[i]->nom, lect->nom) != 0 . 首先,我会考虑(plecteurs[i]->nom != lect->nom) strcmp(plecteurs[i]->nom, lect->nom) != 0的简写。 Same applies to prenom . 同样适用于prenom You should correct this. 您应该对此进行更正。

The snippet 片段

while (j < plecteurs[i]->nb_livres) {
    plecteurs[i]->livres[j]->statut = 0;
    j++;
}

makes the books available without returning them. 使书籍可用而无需归还。 To me this seems unlogical, because the reader may not have given back the book. 对我来说,这似乎是不合逻辑的,因为读者可能还没有退还这本书。 But this may be conforming with the (homework?) assignment you are working at. 但这可能与您正在从事的(作业?)作业相符。 Note that the array of books may have holes your code ignores. 请注意,书本的数组可能会有您的代码忽略的漏洞。 Are all books packed to the beginning of the array when a book is returned? 返回书籍时,是否将所有书籍都打包到数组的开头? In other word, is 换句话说,是

struct Lecteur le4 = { "murche" , "silmeplait" , 2 , {&l4, 0, &l5} };

invalid? 无效?

I didn't test your function but a first superficial look seems that it should compile and with the strcmp() - correction it sould run as you expect. 我没有测试您的功能,但乍看起来似乎应该可以编译并使用strcmp() -可以按预期运行它。 (Your null-lector is invalid, learn the use of NULL pointers). (您的null-lector无效,请学习NULL指针的使用)。

Besides: Why do you use a double semicolon ;; 此外:为什么要使用双分号;; ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM