This presentation is the property of its rightful owner.
Sponsored Links
1 / 57

第 7 章 结构体和共用体 PowerPoint PPT Presentation


  • 129 Views
  • Uploaded on
  • Presentation posted in: General

第 7 章 结构体和共用体. 7.1.1 结构体类型和结构体变量. 数据类型 : 基本类型 : 整型、字符型、浮点型 构造数据类型 : 数组 结构体 共用体 数组实质上是同一类型变量的集合 在实际应用中,一组数据往往具有 不同 的数据类型 , 在 C 语言引入 结构体 来处理这种应用. 以我们常用的通讯录为例,通讯录中的联系人一般要设置序号、姓名、年龄、性别、通信地址等数据,这些数据组合到一起共同来描述和表达联系人这个整体信息。在数据类型说明上,序号为整型;姓名应为字符型;年龄应为整型;性别应为字符型;通信地址应为字符型.

Download Presentation

第 7 章 结构体和共用体

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


7

7


7 1 1

7.1.1

:

:

:

, C .


7


7

  • structure


7 1 2

7.1.2

struct

{};

;


7

struct person

{

long num;

char name[30];

int age;

char sex;

char address[200];

};


7

  • struct person { long num;char *name;

  • int age; char sex;

  • char address[200];};


7 1 3

7.1.3

  • 1struct person{ long num; char name[30];

  • int age; char sex; char address[200];};

  • struct person ab1,ab2;personab1,ab2


7

  • 2struct person { long num; char name[30];

  • int age; char sex; char address[200];}ab1,ab2;


7

  • 3struct { long num; char name[30];

  • int age; char sex; char address[200];}ab1,ab2;


7


7

  • sizeof () :

  • sizeof (struct person);


7

  • :

  • struct contact{ char telephone[20]; char fax[20]; char email[20]; };


7

  • struct person { long num; char name[30];

  • int age; char sex; char address[200];

  • struct contact lxfs;}ab1;


7


7

7.2

  • 7.2.1

  • .ab1,ab2ab1.name ab2.age ..

  • ab1.lxfs.email


7 2 2

7.2.2

  • 7.1

  • #include <string.h>main(){ struct person { long num; char name[30];

  • int age; char sex; char address[200];


7

  • struct person ab1,ab2; ab1.num=1; strcpy(ab1.name,Zhao Jun); printf(input age and sex\n); scanf(%d %c,&ab1.age,&ab1.sex); ab2=ab1; printf("Number=%ld\nName=%s\n" ab2.num,ab2.name); printf("age=%d\nsex=%c\n",ab2.age,ab2.sex);


7

7.3

  • 7.2main(){

  • struct person { long num; char name[30];

  • int age; char sex; char address[200];}ab1={1,Gao Hong",38,M, Beijing Haidian};


7

7.4

  • .4.1

  • struct person

  • {

  • long num;

  • char name[30];

  • int age;

  • char sex;

  • char address[200];

  • }

  • struct person abook[10];


7

  • 7.3.

  • struct person { long num; char name[30];

  • int age; char sex; char address[200];}abook[3]={ {1,"Xu Chong",25,"F",Beijing Chaoyang Distinct }, {2,"Tao Ning",26,"F",Shanghai Fudan University}, {3,Liu Qiang,35,M,Shenzhen Shitong CO.,LTD}

  • }


7

void main()

{ int i;

int avg,sum=0;

for(i=0;i<3;i++)

{sum+=abook[i].age;}

avg=sum/3;

printf(average=%d\n,avg);

}


7

7.5

  • struct *person personpabookstruct person *pabook; struct person ab1;

  • pabook=&ab1pabook=&person


7

  • (*).->(*pabook).namepabook->name


7 5 2

7.5.2

  • psapsa0psa+11psa+ii


7

  • 7.5struct person

  • { long num;

  • char name[30];

  • int age;

  • char sex;

  • char address[200];

  • }abook[3]={

  • {1,"Xu Chong",25,'F',"Beijing Chaoyang Distinct"},

  • {2,"Tao Ning",26,'F',"Shanghai Fudan University"},

  • {3,"Liu Qiang",35,'M',"Shenzhen Dongxing CO.,LTD"} };


7

  • void main()

  • {

  • int i;

  • struct person *psa;

  • printf("Num\t\tName\t\t\tage\t\tsex\t\t\n");

  • psa=abook;

  • for(i=0;i<3;i++)

  • {

  • printf("%ld\t\t%s\t\t%d\t\t%c\t\t\n",psa->num,psa->name,psa->age,psa->sex);

  • psa++;

  • }

  • }


7

  • psa++psa,

  • psa+0 abook[0] {1,"Xu Chong",25,'F',"Beijing Chaoyang Distinct"}

  • psa+1 abook[1] {2,"Tao Ning",26,'F',"Shanghai Fudan University"}psa+2 abook[2] {3,"Liu Qiang",35,'M',"Shenzhen Dongxing CO.,LTD"}


7 5 3

7.5.3

  • 7.630struct person

  • { long num;

  • char name[30];

  • int age;

  • char sex;

  • char address[200];

  • }abook[3]={

  • {1,"Xu Chong",25,'F',"Beijing Chaoyang Distinct"},

  • {2,"Tao Ning",26,'F',"Shanghai Fudan University"},

  • {3,"Liu Qiang",35,'M',"Shenzhen Dongxing CO.,LTD"}};


7

  • void main()

  • {

  • struct person *psa;

  • void average(struct person *inps);

  • psa=abook;

  • average(psa);

  • }

  • void average(struct person *inps)

  • {struct person *tpsa;

  • int c=0,i;

  • int avg,sum=0;

  • tpsa=inps;


7

  • for(i=0;i<3;i++)

  • {

  • sum+=tpsa->age;

  • if(tpsa->age<30) c+=1;

  • }

  • avg=sum/3;

  • printf("average=%d\n persons which age less than thirty count=%d\n",avg,c);

  • }


7

7.6

  • 1) malloc void*malloc(unsigned int size);

  • "size"NULLmallocvoid * char *p; p=(char *)malloc(200*sizeof(char));


7

  • struct person *pab;

  • pab= (struct person*) malloc (sizeof(struct person));


7

  • calloc

  • calloc

  • void*calloc(unsigned int nunsigned int size);

  • nsizecallocmalloc nsize

  • struct person *pab;

  • pab= (struct person*) calloc (2,sizeof(struct person));


7

7.7


7

  • 0numnameage, sex,


7

  • struct person { long num; char name[30];

  • int age; char sex; char address[200];

  • struct person * next;};


7 7 2

7.7.2

7.8,.

  • struct person

  • { long num;

  • char name[30];

  • struct person * next; }

  • struct person *createlist(int n)

    { struct person *head,*cur,*tail;

    /*headcurtail*/

    int i;

    for(i=0;i<n;i++)

  • { cur=(struct person *) malloc(sizeof(struct person)); /**/


7

  • if (cur==NULL)

  • { printf("memory malloc failure");

  • exit(0); }

  • printf("please input number and name\n");

  • scanf("%d,%s",&cur->num,cur->name);

  • if(i==0) tail=head=cur; /**/

  • else tail->next=cur; /**/

  • cur->next=NULL;

  • tail=cur; }

  • return(head); /**/ }


7 7 3

7.7.3

  • 7.9

  • void output_list (struct person *head)

  • {

  • struct person *h;

  • h=head;

  • while(h!=NULL)

  • {

  • printf("%d,%s\n",h->num,h->name);

  • h=h->next;

  • }

  • }


7 7 4

7.7.4

  • numcurcurcurm2m1

  • 1.m2m1cur

  • 2.cur

  • 3.cur


7 7 5

7.7.5

  • :

  • ,curm1cur

  • 1.

  • 2.

  • numheadnum


7

7.8

  • 7.8.2

  • union

  • union {};


7

  • union data

  • {char c;

  • int i;

  • float f;

  • };


7 8 3

7.8.3

  • union data

  • {char c;

  • int i;

  • float f;

  • };

  • union data u1,u2;


7 8 4

7.8.4

  • 1.

  • .

  • union data u1;

  • u1.i=12;

  • u1.f=3.4;

  • 2->

  • ->

  • union data *p;

  • p->i=12;

  • p->f=3.4;


7

7.13

  • union data

  • {char c;

  • int i;

  • float f; };

  • void main()

  • { union data u1;

  • u1.c=a;

  • u1.i=65;

  • printf(%c,u1.i); } /*:A*/

  • f i


7

7.9


7 9 1

7.9.1

  • enum

  • { };enum COLOR{red,green,blue};enum WEEKDAY { sun,mou,tue,wed,thu,fri,sat };


7 9 2

7.9.2

  • a,b,cWEEKDAY

  • enum WEEKDAY { sun,mou,tue,wed,thu,fri,sat };

  • :

  • enum WEEKDAY a,b,c;


7

  • 1..

  • 2.0012WEEKDAYsun0mon1,sat6

  • 3.

  • enum WEEKDAY day;

  • if (day == mon).

  • if (day > sun)


7 10 typedef

7.10 typedef

  • typedef

  • typedef int INTEGER;

  • typdef unsigned int UINT;

  • intunsigned int INTEGERUINTINTEGERint,UINTunsigned int


7

  • typedef struct person{ long num; char name[30];

  • int age; char sex; char address[200]; } TXL, * PTXL;


7

  • TXLstruct personPTXLstruct personTXLPTXL

  • TXL ab1,ab2;

  • struct person ab1,ab2;

  • PTXL pab1,pab2;

  • struct person *pab1,*pab2;


7


  • Login