【问题标题】:Using array structs fuction in C [closed]在C中使用数组结构函数[关闭]
【发布时间】:2019-08-05 10:34:27
【问题描述】:

我有考试。我有排行榜。正如您在 struct 中看到的那样,有团队名称、简称、积分等。我必须使用函数来编写一个 txt 文件,但我不能将函数与数组结构一起使用。我的代码在下面。当我使用这样的功能时,代码块给了我错误。我该如何使用?

(我找到了答案。如果你也需要的话,下面有一个链接。 http://kursattopcuoglu.blogspot.com/2010/06/ilk-donem-final-sorusuna-benzer-bir.html)

struct teams{
char shortname;
char teamname;
int points;
};
void table(struct teams);
int main{
team[2]={'A',"Fenerbahce",0},{'B',"Galatasaray",0};
....
}
void(struct teams team[2]{
printf("%d",team[0].points);

【问题讨论】:

  • 你没有给你的函数一个名字...。 void (struct teams *team) 缺少名称。此外,您应该编辑您的原始问题不添加答案
  • 欢迎来到Stack Overflow,请阅读How to Askminimal reproducible example,了解您的问题为何被否决。
  • 为了帮助人们回答您的问题,您需要更具体地说明错误。请edit 您的帖子包含您从minimal reproducible example 获得的确切错误(最好使用复制+粘贴以避免转录错误)。
  • struct teams team[2]=...替换team[2]=..

标签: c arrays function struct


【解决方案1】:

正确的类型:

    struct teams{
    char shortname;
    char teamname;
    int points;
    };
    void table(struct teams*);
    int main{
    team[2]={'A',"Fenerbahce",0},{'B',"Galatasaray",0};
    ....
    }

void table(struct teams* team){
printf("%d",team[0].points);}

【讨论】:

    【解决方案2】:

    很多答案,但没有一个完整的。以下是:

    struct teams{
        char shortname;
        char *teamname;
        int points;
    };
    void table(struct teams *team);
    
    int main(void){
        struct teams team[2]={{'A',"Fenerbahce",0},{'B',"Galatasaray",0}};
        table(team);
       // ....
    }
    void table(struct teams *team){
        printf("%d",team[0].points);
    }
    

    【讨论】:

      【解决方案3】:

      甚至,您的代码也存在问题。

      考虑到您定义的struct teams,以下初始化器不正确。

      team[2] = {'A',"Fenerbahce",0},{'B',"Galatasaray",0};
      

      要存储像Fenerbahce 这样的字符串,您必须有一个类似于char teamname[20] 的数组作为示例,而不仅仅是char teamname

      您也可以使用char *teamname,但您也必须为其分配一些内存。

      【讨论】:

        【解决方案4】:

        行内:

        /*  .-- function name missing there -- */
            v
        void(struct teams* team){
        printf("%d",team[0].points);}
        

        您缺少函数名称。

        【讨论】:

          猜你喜欢
          • 2020-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-05
          • 2018-12-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多