Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2509915
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-26 09:51:23

    编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输入这些记录,用print函数输出这些记录。
   因为我们知道需要用到5个学生,因此我们需要定义一个关于学生的结构体,定义一个结构体数组。然后使用循环将其输出。在输出时,我们可以使用指向结构体的指针进行操作,代码如下:
 

#include <stdio.h>

struct student
{
       int num;
       char name[10];
       int score[3];
};

void print(struct student *,int);
int main(int argc, char *argv[])
{
    struct student stu[5],*p;
    int i;
    p = &stu;
    
    for (i = 0; i < 5; i++)
    {
        printf("please input %d student info.\n",i + 1);
        scanf("%d%s%d%d%d",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
    }
    printf("the source result:\n");
    
    print(p,5);
    system("pause");
    return 0;
}

void print(struct student *p,int n)
{
     struct student *stu;
     
     int i;
     for (stu = p; stu < p + n; stu++)
     {
         printf("num = %d ,name = %s , score[1] = %d ,score[2] = %d ,score[3] = %d\n",
                     stu->num,stu->name,stu->score[0],stu->score[1],stu->score[2]);
     }
}


阅读(1048) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~