C语言程序设计基础课件 第11章自定义类型_第1页
C语言程序设计基础课件 第11章自定义类型_第2页
C语言程序设计基础课件 第11章自定义类型_第3页
C语言程序设计基础课件 第11章自定义类型_第4页
C语言程序设计基础课件 第11章自定义类型_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言程序设计

第11章自定义类型2num:num[0]num[1]num[2]num[3]num[4]10011002100310041005sex:sex[0]sex[1]sex[2]sex[3]sex[4]'M''F''M''F''F'score:score[0]score[1]score[2]score[3]score[4]85.090.092.070.079.00'Z''h''a''o''\0''\0''\0''\0''\0''\0'1'Q''i''a''n''\0''\0''\0''\0''\0''\0'2'S''u''n''\0''\0''\0''\0''\0''\0''\0'3'L''i''\0''\0''\0''\0''\0''\0''\0''\0'4'Z''h''o''u''\0''\0''\0''\0''\0''\0'name:管理学生成绩constintN=5;intnum[N]; //学号charname[N][10]; //姓名charsex[N]; //性别floatscore[N]; //分数不同信息分散在不同变量,不便操作,且容易出错。概述?能否用一个变量来保存一个学生的学号、姓名、性别、分数的所有信息呢?结构体自定义类型——结构体intfloatdoublechar自定义类型允许自己设计新的数据类型,定义了新类型后,就能用新类型定义变量。structstu{

intnum;

charname[10];

charsex;

floatscore;};定义一种自定义数据类型类型没有内存空间,不能保存数据structstuboy1;intfloatdoublecharstructstu

这是一个变量不是定义变量!不是定义变量,而是设计成员(各成员的类型可以不同)定义新类型的变量,开辟内存空间numnamesexscoreboy1:stuboy1;使用结构体变量structstu{

intnum;

charname[10];

charsex;

floatscore;};structstuboy1;numnamesexscoreboy1:boy1=1001;scanf(%d",&boy1);printf("%d",boy1);boy1.num=1001;boy1.sex='M';boy1.score=85.0;="Zhao";strcpy(,"Zhao");printf("%d\n",boy1.num);printf("%f\n",boy1.score);printf("%s\n",);printf("%c\n",[1]);结构体变量名.成员名numnamesexscore1001numnamesexscore1001'M'numnamesexscore1001'M'85.0name是数组名,是假想的指针变量,值不能改1001

100185.000000

100185.000000zhao100185.000000zhaoh?????\0oahz结构体类型和结构体变量的区别结构体类型boy1:numnamesexscorestructstu{

intnum;

charname[10];

charsex;

floatscore;};boy2:numnamesexscoreprintf("%d",sizeof(boy1));printf("%d",sizeof(boy2));printf("%d",sizeof(structstu));printf("%d",sizeof(stu));202020特殊地,sizeof对类型和变量均可使用stu.num=1001;printf("%f",stu.score);boy1.num=1001;printf("%f",boy2.score);结构体类型的变量产品有内存空间,可以保存数据类型叫structstu,不叫stustructstu{

intnum;

charname[10];

charsex;

floatscore;};图纸不占内存,不能保存数据先定义类型,然后才能定义变量结构体类型变量的定义方式①先定义类型,再定义变量structstu{

intnum;

charname[10];

charsex;

floatscore;};structstuboy1,boy2;structstu{

intnum;

charname[10];

charsex;

floatscore;}boy1,boy2;②定义类型的同时,定义变量③定义类型的同时定义变量,但省略类型名

struct{

intnum;

charname[10];

charsex;

floatscore;}boy1,boy2;structstuboy3;无法再定义其他变量如boy3,因类型无名结构体类型变量的初始化在定义结构体变量时也可对结构体变量赋初值,方法是将各成员的值按类型定义时的各成员顺序,依次放在一对{}内。structstuboy2,boy1={1001,"zhao",'M',85.0};numnamesexscoreboy1:numnamesexscore1001numnamesexscorenumnamesexscore1001'M'85.0boy2:scoresexnamenum??????????????????\0oahzstructstu{

intnum;

charname[10];

charsex;

floatscore;};结构体变量之间的赋值structstu{

intnum;

charname[10];

charsex;

floatscore;};numnamesexscoreboy1:numnamesexscore1001numnamesexscore1001'M'numnamesexscore1001'M'85.0?????\0oahzboy2=boy1;可以整体赋值:所有成员一次性全部赋值,因为是2个变量boy2:scoresexnamenum?????????????85.0'M'1001?????\0oahz结构体类型的数组数组的元素也可以是结构体类型的,称为结构体数组。每个元素都是相同结构体类型的变量。一本通讯录,每一页是一个结构体变量,一本就是一个结构体数组。structstuss[3];ss:numnamesexscore????numnamesexscore????numnamesexscore????ss[0]ss[1]ss[2]ss[0].num=1001;strcpy(ss[1].name,"Qian");ss[2].score=92.0;1001Qian\092.0结构指针变量一个指针变量当用来指向一个结构体类型的变量时,称结构指针变量。structstuboy1;structstu*p;p=&boy1;p=&stu;stu是类型,不是变量,没有内存空间,更无地址p=&boy1.num;boy1.num是int,p基类型是stu不是int,不能指向int

若:int*q;

q=&boy1.num;正确1000p:[]2000numnamesexscoreboy1:p:[1000]2000通过结构指针变量访问结构体变量p->num=1001;boy1.num=1001;strcpy(p->name,"Zhang");strcpy(boy1.name,"Zhang");(*p).score=92.0;boy1.score=92.0;结构体变量用

.结构体指针变量用->(*结构体指针变量)

.*p.score.

和->

的优先级都最高(与()相当),这相当于*92.0,故错误1000numnamesexscoreboy1:p:[1000]2000structstuboy1;structstu*p;p=&boy1;一个指针变量当用来指向一个结构体类型的变量时,称结构指针变量。100192.0\0????gnahz结构体指针指向结构体数组结构指针变量也可以指向一个结构体数组。ps+1或ps-1会移动一个元素(一个“结构体变量”)所占字节数,与普通数组的一致。structstux[3];structstu*ps=x;ps++;100010201040x:numnameage1Zhang\020numnameage2Wang\019numnameage3Zhao\018x[0]x[1]x[2]x:[1000]1000

ps:[1000]2000

[1020]

ps++;[1040]

x+2的值为:1040结构体类型数据做函数参数1)重口味:结构体类型变量作函数参数fun(structstua){…}2)轻口味:结构体指针作函数参数fun(structstu*p){…}全部成员整体传送fun的空间main的空间numnamesexscore1001Zhao\0'M'85.0numnamesexscore1001Zhao\0'M'85.0形参:实参:fun的空间main的空间numnamesexscore1001Zhao\0'M'85.0形参:实参:[1000]20001000只传送一个地址(4字节)时间和空间开销都很大;但在函数中如果改变了形参的某个成员的值,不影响实参。无论成员多少,只传4字节,提高了效率;但在函数中如果改变了形参所存地址所对应空间的值,实参变量的值就被改变。结构体变量作函数参数fun的空间

main的空间numnamesexscore1003Sun\0'M'92.0s:numnamesexscore1003Sun\0'M'92.0t:1003SunM921003SunM921003SunM92102Zhang\0'F'90.0#include<string.h>structperson{ intnum;

charname[10];

charsex;

floatscore;};voidfun(structpersont)//传递变量{ t.num=102; strcpy(,"Zhang"); t.sex='F'; t.score=90;}main(){ structpersons={101,"Li",'M',75};

printf("(1)S:%d%s%c%f\n", s.num,,s.sex,s.score); fun(s);

printf("(2)S:%d%s%c%f\n", s.num,,s.sex,s.score);}结构体指针作函数参数#include<string.h>structperson{ intnum;

charname[10];

charsex;

floatscore;};voidfun(structperson*ps)/*传递指针*/{ ps->num=102; strcpy(ps->name,"Zhang");

ps->sex='F';ps->score=90;}main(){ structpersons={101,"Li",'M',75};

printf("(1)S:%d%s%c%f\n", s.num,,s.sex,s.score);

fun(&s);

printf("(2)S:%d%s%c%f\n", s.num,,s.sex,s.score);}main的空间

numnamesexscore101Li\0'M'75.0s:fun的空间

ps:[1000]20001000102Zhang\0'F'90.0(1)S:101LiM75.000000(1)S:101LiM75.000000(2)S:102ZhangF90.000000#include<stdio.h>#include<string.h>structA{inta;charb[10];doublec;};structAf(structAt);main(){structAa={1001,"Zhang",1098.0};a=f(a);printf("%d,%s,%6.1f\n",a.a,a.b,a.c);}structAf(structAt){t.a=1002;strcpy(t.b,"Chang");t.c=1202.0;returnt;}main的空间

F1的空间

结构体类型的变量作为返回值a:abc10011098.01000Zhang\0\0\0\0\0t:abc10011098.02000Zhang\0\0\0\0\01002,Chang,1202.01002Chang\01202.010021202.0Chang\0\0\0\0\01002Chang\01202.0structstu{ intnum;

charname[10];

charsex;

struct { intyear; intmonth; intday; }birthday;

floatscore;};structstu{ intnum;

charname[10];

charsex;

structdate { intyear; intmonth; intday; }birthday;

floatscore;};结构体类型的嵌套structdate{ intyear; intmonth; intday;};structstu{ intnum; charname[10];

charsex; structdatebirthday;

floatscore;};可省略“日期”结构体名date或:structstua,*p=&a;a.birthday.year=2002;p->birthday.month=12;(*p).birthday.day=21;numnamesexbirthdayscoreyearmonthdaya:p:[1000]2000100020022112类型定义符typedef类型定义语句typedeftypedef为类型起“绰号”typedefint

INTEGER;这以后就可用INTEGER来代替int作类型说明了INTEGERa,b;inta,b;typedef

某类型“变量”定义形式;并不产生新的数据类型,只是给已有的类型增加新名;“变量”相当的位置为新类型名。typedefdouble

real;reala,b;doublea,b;typedefint*

INTPTR;INTPTRp;int*p;typedefunsignedlongUINT32;UINT32x,y;unsignedlongx,y;这以后就可用real来代替double作类型说明了这以后就可用INTPTR来代替int*作类型说明了

这以后就可用UINT32来代替unsignedlong作类型说明了用typedef为结构体类型起别名typedefstructstu{intnum;charname[10];charsex;floatscore;}

ST;ST

boy1,boy2;structstugirl1,girl2;ST

boy1,boy2;STgirl1,girl2;动态存储分配C语言常用内存管理库函数(包含stdlib.h)

函数功能用法malloc分配1块长度为size字节的连续内存空间(不清零),函数返回该空间的首地址;如分配失败函数返回0(类型说明符*)malloc(size)calloc分配n块、每块长度为size字节的连续内存空间(共size×n字节),并将该空间中的内容全部清零,函数返回该空间的首地址;如分配失败函数返回0(类型说明符*)calloc(n,size)free释放ptr所指向的一块内存空间,ptr是由malloc或calloc函数所分配空间的地址,即是这两个函数的返回值(或类型转换后的返回值)free(ptr)(ptr为任意基类型的指针)动态存储分配例float*q,*r;char*pc;structstudent*ps;q=(float

*)malloc(4);

/*分配4字节的空间,用于保存float型数据*/pc=(char

*)malloc(100);

/*分配100字节的空间,用于保存char型数组*/r=(float

*)calloc(5,4); /*分配5块每块4字节的空间,共20字节

用于保存含5个元素的float型数组*/ps=(structstudent*)calloc(10,

sizeof(structstudent)); /*分配10块每块为一个structstudent类型数据大小的空间,用于保存10个元素的structstudent型数组*/练习#include<stdio.h>#include<string.h>#include<stdlib.h>main(){ char*p;inti; p=(char*)malloc(sizeof(char)*20);

strcpy(p,"welcome");

for(i=6;i>=0;i--) putchar(*(p+i)); printf("\n");

free(p);}1000...'w''e''l''c''o''m''\0''e'p:[1000]2000

100610071001eememoemoclew链表链表18[0]200019[4000]500015[]400012[2000]1000datanextdatanextdatanext1000

datanext链表各元素的逻辑结构与存储结构一般不一致。以链接方式存储的线性表structnode{ intdata;

structnode*next;};datanextX[5000]头结点一般增加头结点以方便编程3000链表处理编程套路链表处理编程套路(设结点结构体类型别名为SNODE,成员为data和next;头结点指针为h):

SNODE*p;

/*有时类型别名为:NODE、SLIST、STYPE等,视题目而定*/

p=h->next;while(p) /*或写为:while(p!=0)注意不是

while(*p)*/{

处理p->data;/*有时数据成员名为:s,视题目而定*/ p=p->next; }链表的遍历/*依次输出链表中的数据,h是头结点的地址*/voidoutlist(SNODE*h){ SNODE*p; p=h->next;

while(p) /*也可写为while(p!=0)*/ { printf("%d",p->data); p=p->next; }}main(){ inta[N]={19,15,12,18,11};

SNODE*head;/*head保存头结点的地址*/ head=createlist(a); /*建立链表*/ outlist(head); /*输出链表*/ destroylist(head); /*销毁链表*/}习题请编写函数sumlist,其原型如下:intsumlist(SNODE*h)函数的功能是求上例建立的链表中各结点数据域之和,并由函数返回,形参h指向链表的头结点。使main函数对此函数的调用为printf("%d",sumlist(head));则能输出和。

intsumlist(SNODE*h){ SNODE*p;ints=0; p=h->next;

while(p) { s+=p->data; p=p->next; }

returns;}习题2请编写函数maxlist,其原型如下:intmaxlist(SNODE*h)函数的功能是求上例建立的链表中各结点数据域中

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论