鱼C论坛

 找回密码
 立即注册
查看: 3807|回复: 2

C语言-结构体与共用体问题

[复制链接]
发表于 2013-2-17 17:46:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
        int num;
        char name[20];
        float score[3];
};
void print(struct student);
void main()
{
        struct student stu[2];
        strcpy(stu[0].name,"小红");
        strcpy(stu[1].name,"小黄");
        stu[1].score[0]=56.5;
        stu[1].score[1]=66.5;
        stu[1].score[2]=46.5;
        stu[2].score[0]=76.5;
        stu[2].score[1]=86.5;
        stu[2].score[2]=96.5;
        print(stu[2]);
       
}
void print(struct student stu[2])
{
        printf("%s 1 is %f %f %f",stu[0].name,stu[0].score[0],stu[0].score[1],stu[0].score[2]);
        printf("%s 1 is %f %f %f",stu[1].name,stu[1].score[0],stu[1].score[1],stu[1].score[2]);
}


编译出错了,大神帮忙看看。我是要把多个结构体的变量传输到函数中。可是编译出错,大神帮忙看下~

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-17 20:08:11 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. struct student
  5. {
  6.          int num;
  7.          char name[20];
  8.          float score[3];
  9. };
  10. //void print(struct student);
  11. void print(struct student*);
  12. void main()
  13. {
  14.          struct student stu[2];
  15.          strcpy(stu[0].name,"小红");
  16.          strcpy(stu[1].name,"小黄");
  17.                  /*
  18.          stu[1].score[0]=56.5;
  19.          stu[1].score[1]=66.5;
  20.          stu[1].score[2]=46.5;
  21.          stu[2].score[0]=76.5;
  22.          stu[2].score[1]=86.5;
  23.          stu[2].score[2]=96.5;
  24.          print(stu[2]);
  25.                  */
  26.                  stu[0].score[0]=56.5;
  27.          stu[0].score[1]=66.5;
  28.          stu[0].score[2]=46.5;
  29.          stu[1].score[0]=76.5;
  30.          stu[1].score[1]=86.5;
  31.          stu[1].score[2]=96.5;
  32.          print(stu);
  33.          
  34. }
  35. void print(struct student stu[2])
  36. {
  37.          printf("%s 1 is %f %f %f",stu[0].name,stu[0].score[0],stu[0].score[1],stu[0].score[2]);
  38.          printf("%s 1 is %f %f %f",stu[1].name,stu[1].score[0],stu[1].score[1],stu[1].score[2]);
  39. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-8 20:35:28 | 显示全部楼层
数组是从0开始算的
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-8 05:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表