结构体
来源:互联网 发布:java读取xml节点内容 编辑:程序博客网 时间:2024/05/01 08:57
结构体指针的定义和引用
指针变量非常灵活方便,可以指向任一类型的变量,若定义指针变量指向结构体类型变
量,则可以通过指针来引用结构体类型变量。
7.3.1 指向结构体类型变量的使用
首先让我们定义结构体:
struct stu
{
char name[20];
long number;
float score[4];
} ;
再定义指向结构体类型变量的指针变量:
struct stu *p1, *p2 ;
定义指针变量p 1、p 2,分别指向结构体类型变量。引用形式为:指针变量→成员;
[例7-2] 对指向结构体类型变量的正确使用。输入一个结构体类型变量的成员,并输出。
#include <stdlib.h> /*使用m a l l o c ( ) 需要* /
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
7.3.2 指向结构体类型数组的指针的使用
定义一个结构体类型数组,其数组名是数组的首地址,这一点前面的课程介绍得很清楚。
定义结构体类型的指针,既可以指向数组的元素,也可以指向数组,在使用时要加以区分。
[例7-3] 在例7 - 2中定义了结构体类型,根据此类型再定义结构体数组及指向结构体类型的指针。
struct data
{
intday,month,year;
};
struct stu/*定义结构体*/
{
char name[20];
long num;
struct data birthday;/嵌*套的结构体类型成员*/
};
struct stustudent[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等。
[例7-4]指向结构体数组的指针变量的使用。
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);
}
///*************************************一下为自己例程**************************************************//
7.3.3同一文件中定义两个结构体时,其中两个结构体互相包含:
注意函数指针的参数。。。。。。
struct _stream;
52 typedef struct _selectlist{
53 undolist listnum;
54 int (*_changefoc_f) (undolist _listnum,int _changefoc_a);
55 int (*_zhuaction_f) (struct _stream *streamdemo,undolist _listnum);
56 struct _selectlist *head,*tail;
57 }selectlist;
58
59
60 struct _stream{
61 undolist undolist;
62
63 int (*_creatlist_f)(undolist listnum,
64 int hyaline,
65 int layerwidth,
66 int layerheith,
67 int position_x,
68 int position_y,
69 int out
70 );
71 int (*_writezhubiaoti_f)(undolist writezhubiaoti_a,char *sum_a);
72 int (*_selectzhu_f)(struct _stream *streamdemo,selectlist *firstfp);
73 };
74 typedef struct _stream stream;
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体
- 结构体,,,,
- 结构体
- 结构体
- Android Display System --- Surface Flinger
- 在windows下查找端口的cmd中的命令
- UIButton的使用
- Ubuntu下用C语言访问MySQL数据库(转载)
- RedHat Linux vsftpd root用户上传下载 配置中遇到问题及解决办法
- 结构体
- C++中定义类的对象:用new和不用new有何区别?
- 体验win7的绚丽
- 天气凉爽,开始锻炼身体
- 字符编码笔记:ASCII,Unicode和UTF-8 - 阮一峰的网络日志
- j2se学习要点记录(一)面向对象
- 黑马程序员_集合和IO学习
- linux程序编译
- java设计模式---Template Method模板方法---实际处理交给子类