开发者社区> 问答> 正文

c语言结构体数组无法运行?

#include <stdio.h>
#include <stdlib.h>

void passOrdown(struct student *);
void output(struct student *);

struct student {
                char name[20];
                int score;
                char *passdown;
        };

int main()
{
        struct student stu[3];
        int i;
        for (i=0; i<3; i++) {
        printf("请输入第#%d位同学的姓名: ", i+1);
        scanf("%s", stu[i].name);

        printf("请输入第#%d位同学c语言的分数:", i+1);
        scanf("%d", &stu[i].score);
        printf("\n");
        }
        passOrdown(stu);
        output(stu);
        return 0;
}
void passOrdown(struct student *p)
{
        int i;
        for (i=0; i<3; i++) {
                if (p->score >= 60)
                        p->passdown = "pass";
                else
                        p->passdown = "down";
        }
}

void output(struct student *q)
{
        int i;
        printf("\n\n%10s %10s %20s\n", "Name", "Score", "Passdown");
        for (i=1; i<42; i++)
                printf("=");
        printf("\n");
        for (i=0; i<3; i++) {
                printf("%10s %10d %20s\n", q->name, q->score, q->passdown);
                q++;
        }
}

展开
收起
a123456678 2016-06-07 19:24:01 2151 0
1 条回答
写回答
取消 提交回答
  • 改动部分及原因见注释,以下代码可以直接运行

    #include <stdio.h>
    #include <stdlib.h>
    
    struct student {
                    char name[20];
                    int score;
                    char *passdown;
            };
    
    void passOrdown(struct student *);   // 这里用到了student,要先声明,所以student
    void output(struct student *);       // 的定义要放在前面,然后才是函数声明
    
    int main()
    {
            struct student stu[3];
            int i;
            for (i=0; i<3; i++) {
            printf("请输入第#%d位同学的姓名: ", i+1);
            scanf("%s", stu[i].name);
    
            printf("请输入第#%d位同学c语言的分数:", i+1);
            scanf("%d", &stu[i].score);
            printf("\n");
            }
            passOrdown(stu);
            output(stu);
            return 0;
    }
    void passOrdown(struct student *p)
    {
            int i;
            for (i=0; i<3; i++) {
                    if (p[i].score >= 60)      // p是student的数组 ,不是一个student
                            p[i].passdown = "pass";
                    else
                            p[i].passdown = "down";
            }
    }
    
    void output(struct student *q)
    {
            int i;
            printf("\n\n%10s %10s %20s\n", "Name", "Score", "Passdown");
            for (i=1; i<42; i++)
                    printf("=");
            printf("\n");
            for (i=0; i<3; i++) {
                    printf("%10s %10d %20s\n", q->name, q->score, q->passdown);
                    q++;
            }
    }
    2019-07-17 19:30:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载