Chinaunix首页 | 论坛 | 博客
  • 博客访问: 365861
  • 博文数量: 181
  • 博客积分: 215
  • 博客等级: 民兵
  • 技术积分: 313
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 19:39
个人简介

王的男人

文章分类

全部博文(181)

文章存档

2016年(2)

2015年(35)

2014年(17)

2013年(84)

2012年(49)

我的朋友

分类:

2012-07-19 00:16:11

//对指向结构体类型变量的正确使用。输入一个结构体类型变量的成员,并输出。

#include /*使用malloc()需要* /

struct data / *定义结构体* /
{
   int day,month,year;
} ;

struct stu /*定义结构体* /
{
    char name[20];
    long num;
    struct data birthday; /嵌*套的结构体类型成员*/
} ;

main() /*定义m a i n ( ) 函数* /
{
     struct stu *student; 定/*义结构体类型指针*/
     student=malloc(sizeof(struct stu)); /* 针变量分配安全的地址*/
     printf("Input name,number,year,month,day:\n");
     scanf("%s",student->name); 输/*入学生姓名、学号、出生年月日*/
     scanf("%ld",&student->num);
     scanf("%d%d%d",&student->birthday.year,&student->birthday.month,
     &student->birthday.day);
     printf("\nOutputname,number,year,month,day\n");
     /*打印输出各成员项的值*/
     printf("%20s%10ld%10d//%d//%d\n",student->name,student->num,
     student->birthday.year,student->birthday.month,
     student->birthday.day);
}

程序中使用结构体类型指针引用结构体变量的成员,需要通过C提供的函数malloc()来为指针分配安全的地址。函数sizeof()返回值是计算给定数据类型所占内存的字节数。指针所指各成员形式为:
student->name
student->num
student->birthday.year
student->birthday.month
student->birthday.day

===============================

struct data
{
    intday,month,year;
};

struct stu/*定义结构体*/
{
    char name[20];
    long num;
    struct data birthday;/嵌*套的结构体类型成员*/
};

struct stu student[4],*p;定/*义结构体数组及指向结构体类型的指针*/

作p=student,此时指针p就指向了结构体数组student。
p是指向一维结构体数组的指针,对数组元素的引用可采用三种方法。
1)地址法
student+i和p+i均表示数组第i个元素的地址,数组元素各成员的引用形式为:
(student+i)->name、(student+i)->num和(p+i)->name、(p+i)->num等。student+i和p+i
与&student[i]意义相同。
2)指针法
若p指向数组的某一个元素,则p++就指向其后续元素。
3)指针的数组表示法
若p=student,我们说指针p指向数组student,p[i]表示数组的第i个元素,其效果与
student[i]等同。对数组成员的引用描述为:p[i].name、p[i].num等。

//程序示例:

structdata/*定义结构体类型*/
{
     intday,month,year;
};

structstu/*定义结构体类型*/
{
     char name[20];
     long num;
     struct data birthday;
};

main()
{

     inti;
     structstu*p,student[4]=

     {

         {"liying",1,1978,5,23},

         {"wangping",2,1979,3,14},
         {"libo",3,1980,5,6},

         {"xuyan",4,1980,4,21}

     };
/*定义结构体数组并初始化*/
     p=student;/*将数组的首地址赋值给指针p,p指向了一维数组student*/
     printf("\n1----Outputname,number,year,month,day\n");
     for(i=0;i<4;i++)/*采用指针法输出数组元素的各成员*/
     printf("%20s%10ld%10d//%d//%d\n",(p+i)->name,(p+i)->num,
     (p+i)->birthday.year,(p+i)->birthday.month,
     (p+i)->birthday.day);
}




//结构指针变量作函数参数

#include

struct stu

{

     int num;

     char *name;

     char ***;

     float score;

}boy[5]=

     {

     {101,"Li ping",'M',45},

     {102,"Zhang ping",'M',62.5},

     {103,"He fang",'F',92.5},

     {104,"Cheng ling",'F',87},

     {105,"Wang ming",'M',58},

     };

main()

{

     struct stu *ps;

     void ave(struct stu *ps);

     ps=boy;

     ave(ps);

}

void ave(struct stu *ps)

{

     int c=0,i;

     float ave,s=0;

     for(i=0;i<5;i++,ps++)

     {

           s+=ps->score;

           if(ps->score<60) c+=1;

     }

     printf("s=%f\n",s);

     ave=s/5;

     printf("average=%f\ncount=%d\n",ave,c);

}

测试环境:VC++6.0

输出结果:

s=345.000000

average=69.000000

count=2

Press any key to continue

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