【问题标题】:function with array with pointer as parameter以指针为参数的数组函数
【发布时间】: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


【解决方案1】:

问题出在函数void desabonnement(struct Lecteur * plecteurs[], int * nombre_lecteurs, struct Lecteur * lect) 的行while (i &lt; * nombre_lecteurs) 中。这应该是while (i+1 &lt; * nombre_lecteurs)

查看完整的工作代码here

注意:在您的实际代码中,您应该在删除完成后减小le_nombre 以反映新的大小(我已经在corrected code here 中这样做了)。

【讨论】:

    【解决方案2】:

    其实你的问题是什么?

    只是一些评论:

    首先我认为(plecteurs[i]-&gt;nom != lect-&gt;nom)strcmp(plecteurs[i]-&gt;nom, lect-&gt;nom) != 0。同样适用于prenom。 你应该纠正这个。

    sn-p

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

    在不归还书籍的情况下提供书籍。在我看来,这似乎不合逻辑, 因为读者可能还没有归还这本书。但这可能是 符合您正在从事的(家庭作业?)任务。 请注意,这一系列书籍可能有您的代码忽略的漏洞。 当一本书返回时,所有的书是否都打包到数组的开头? 也就是说,是

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

    无效?

    我没有测试你的功能,但乍一看似乎应该 编译并使用strcmp() - 更正它会按照您的预期运行。 (你的null-lector无效,学习NULL指针的使用)。

    另外:为什么要使用双分号;;

    【讨论】:

    • 您好,感谢您的发言!对于分号,这是一个错误,我没有看到它!其余的,正如你所说,这是一个有一些特定规格的家庭作业!!
    猜你喜欢
    • 2010-12-15
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    相关资源
    最近更新 更多