You are on page 1of 34

MN HC

Ging vin: Nguyn c Hong B mn iu Khin T ng Khoa in in T i Hc Bch Khoa Tp.HCM Email: ndhoang@hcmut.edu.vn

CHNG 13

CC KIU D LIU C CU TRC KIU D LIU T NH NGHA

Ni dung chng 13
13.1 13.2 13.3 13.4 Kiu struct Kiu union Kiu enum nh ngha kiu bng typedef

Kiu struct

Khi nim Khai bo struct


Cu trc (struct) l mt kiu d liu phc hp c to t cc kiu d liu khc, cc kiu d liu ny c s dng khai bo cho cc bin thnh phn ca bin kiu struct C php nh ngha mt cu trc struct ten_cau_truc { khai bo cc bin thnh phn; (bin, mng, cu trc nh ngha) };

Khi nim Khai bo struct


VD1: struct ngay { unsigned ngaythu; unsigned thang; unsigned nam; }; VD2: struct nhancong { char ten[15]; char diachi[40]; double bacluong; struct ngay ngaysinh; struct ngay ngaycongtac; };

Khi nim Khai bo struct


Vic nh ngha mt cu trc mi ch bo cho b dch C bit l c mt cu trc c nh ngha, ch cha cp ch cho bin cu trc c bin cu trc ta phi khai bo bin vi kiu cu trc nh ngha C php khai bo mt bin cu trc struct ten_cau_truc ten_bien; truy xut n mt thnh phn ca bin cu trc ta s dng ton t chm .

Khi nim Khai bo struct


VD3: #include "conio.h #include "stdio.h" struct ngay { unsigned ng; unsigned th; unsigned na; }; main() { struct ngay ns; unsigned d,m,y; printf("Moi nhap ngay sinh theo mau (dd mm yy): "); scanf("%u%u%u",&d,&m,&y); ns.ng = d; ns.th = m; ns.na = y; printf("\nBan sinh ngay: %u/%u/%u",ns.ng,ns.th,ns.na); getch(); }

VD4:#include "conio.h #include "stdio.h #include "string.h" struct ngay { unsigned ng; unsigned th; unsigned na; }; struct sinhvien { char ht[30]; char mssv[10]; struct ngay ns; } sv; main() { printf("Moi SV nhap: "); printf("\nHo ten: "); gets(sv.ht); printf("\nMSSV: "); gets(sv.mssv); printf("\nNgay sinh (dd mm yy): "); scanf("%u%u%u",&sv.ns.ng,&sv.ns.th,&sv.ns.na); printf("\nSV %s co mssv %s \nsinh ngay: %u/%u/%u", sv.ht,sv.mssv,sv.ns.ng,sv.ns.th,sv.ns.na); getch(); }

Khi nim Khai bo struct


C cho php gn cc cu trc cng kiu cho nhau qua tn bin cu trc thay v phi gn tng thnh phn cho nhau C cho php i s ca hm l struct, c truyn theo tham s tr Kiu struct c th tnh ton kch thc theo byte bng ton t sizeof

VD5:#include "conio.h #include "stdio.h #include "string.h" struct ngay { unsigned ng; unsigned th; unsigned na; } ; struct sinhvien { char ht[30]; char mssv[10]; struct ngay ns; } sv,svc; void in(struct sinhvien sv); main() { printf("Moi SV nhap: "); printf("\nHo ten: "); gets(sv.ht); printf("\nMSSV: "); gets(sv.mssv); printf("\nNgay sinh (dd mm yy): "); scanf("%u%u%u",&sv.ns.ng,&sv.ns.th,&sv.ns.na); svc=sv; in(svc); getch(); } void in(struct sinhvien sv) { printf("\nSV %s co mssv %s \nsinh ngay:\ %u/%u/%u",sv.ht,sv.mssv,sv.ns.ng,sv.ns.th,sv.ns.na); }

Mng cc struct
C cho php khai bo mng struct nh mng cc kiu d liu khc C php: struct ten_struct ten_bien_struct[kichthuoc]; VD: struct sinhvien sv[5];

VD6 : #include "conio.h #include "stdio.h #include "string.h" struct ngay { unsigned ng; unsigned th; unsigned na; } ; struct sinhvien { char ht[30]; char mssv[10]; struct ngay ns; } sv[3]; void in(struct sinhvien sv); main() { int i; for(i=0;i<3;i++) { printf("\nMoi SV thu %d nhap: ",i+1); printf("\nHo ten: "); gets(sv[i].ht); printf("\nMSSV: "); gets(sv[i].mssv); printf("\nNgay sinh (dd mm yy): "); scanf("%u%u%u",&sv[i].ns.ng,&sv[i].ns.th,&sv[i].ns.na); flushall(); } printf("\nDanh sach gom co %d SV:",i); for(i=0;i<3;i++){ printf("\n%d.",i+1); in(sv[i]);} getch(); } void in(struct sinhvien sv) { printf("SV %s co mssv %s sinh ngay:\ %u/%u/%u",sv.ht,sv.mssv,sv.ns.ng,sv.ns.th,sv.ns.na); }

Pointer ti mt struct
Mt bin struct sau khi khai bo s c mt a ch xc nh trong b nh. C th ly a ch ny bng ton t & a ch ny l mt hng pointer tr n u ca cu trc, ta c th khai bo mt pointer tr ti mt cu trc C php: struct ten_struct *ten_pointer; VD: struct sinhvien sv,msv[5],*psv; psv = &sv; psv = msv;

VD7 : #include "conio.h #include "stdio.h #include "string.h" struct ngay { unsigned ng; unsigned th; unsigned na; }; struct sinhvien { char ht[30]; char mssv[10]; struct ngay ns;} sv[3],*psv; void nhap(struct sinhvien *psv); void xuat(struct sinhvien sv); main() { int i; psv=sv; for(i=0;i<3;i++){ printf("\nMoi SV thu %d nhap: ",i+1); nhap(psv+i); flushall(); } printf("\nDanh sach gom co %d SV:",i); for(i=0;i<3;i++){ printf("\n%d.",i+1); xuat(sv[i]); } getch(); } void nhap(struct sinhvien *psv) { printf("\nHo ten: "); gets(psv->ht); printf("\nMSSV: "); gets(psv->mssv); printf("\nNgay sinh (dd mm yy): "); scanf("%u%u%u",&psv->ns.ng,&psv->ns.th,&psv->ns.na);} void xuat(struct sinhvien sv) { printf("SV %s co mssv %s sinh ngay:\ %u/%u/%u",sv.ht,sv.mssv,sv.ns.ng,sv.ns.th,sv.ns.na);}

Struct dng field


C cho php khai bo cc thnh phn struct theo bit hoc nhm bit struct dng field C php: struct ten_struct { kieu ten_field1: so_bit1; kieu ten_field2: so_bit2; ... } ten_bien; kieu: signed, unsigned, int ten_field1, ten_field2: ten cc field thnh phn ca struct so_bit1, so_bit2: quy nh s bit nhm thnh 1 field (116)

Struct dng field


VD: struct date { unsigned day: 5; unsigned month: 4; unsigned year: 6; } ngay;

Struct dng field


di mi field khng qu 16 bit VD: struct vd { unsigned field1: 7; unsigned field2: 5; unsigned field3: 2; unsigned field4: 6; unsigned field5: 7; } vd1;

Struct dng field


-Khng th ly a ch ca bin kiu field -Khng th khai bo mng cc field -Khng th tr tr ca bin field qua tn hm bng return -Nu field c khai bo l c du m ch c mt bit th n ch c 2 gi tr l 0 v -1 s dng bin trung gian

VD29 : #include "conio.h #include "stdio.h" struct vd { unsigned a:4; unsigned b:5; }; unsigned sum(struct vd s) { unsigned tam; tam = s.a + s.b; return tam; } void nhap(unsigned *t) { scanf("%u",t); } main() { unsigned tam; struct vd vd1; printf("\nNhap so thu nhat: "); nhap(&tam); vd1.a = tam; printf("\nNhap so thu hai : "); nhap(&tam); vd1.b = tam; printf("\nTong 2 so la: %u",sum(vd1)); getch();}

Kiu Union

Kiu Union
y l kiu d liu c bit m nu c khai bo th ng vi mt vng nh, gi tr mi thi im khc nhau th c th c kiu khc nhau ty vo vic s dng bin thnh phn trong n C php: union ten_union { khai_bao_bien_thanh_phan; }; Khai bo bin thnh phn l danh sch lit k cc tn bin cng cc kiu c th s dng chung vng nh c cp

Kiu Union
VD: union test { char c; int i; float f; double d; }; Tt c cc bin c, i, f, d u c cng mt vng nh c cp nn mi php gn, thay i tr trn mt thnh phn u nh hng n cc thnh phn khc

Kiu Union
Bin union thc cht l bin mt phn t nhng mi thi im s dng khc nhau phn t ny c th c hiu l mt thnh phn no trong cc thnh phn nh ngha ca union C php: union ten_union { khai_bao_bien_thanh_phan; }; Khai bo bin thnh phn l danh sch lit k cc tn bin cng cc kiu c th s dng chung vng nh c cp

VD: #include "conio.h #include "stdio.h" union test { char c; int i; float f; double d; }; main() { union test a; a.c = 'A'; a.i = 10; a.f = 1.5; a.d = 1e3; printf("%f",a.f); getch(); }

VD: #include "conio.h #include "stdio.h" union test { char c; int i; }; main() { union test a,*pa; pa = &a; a.c = 'A'; a.i = 67; printf("%c",pa->c); getch(); }

VD30: #include "conio.h #include "stdio.h" union test { struct { unsigned a1; unsigned a2; }s; struct { unsigned n1:1; unsigned n2:1; unsigned n3:8; }f; }u; main() { u.s.a1 = 0x16; printf("%d\n",u.f.n1); printf("%d\n",u.f.n2); getch();}

Kiu Enum

Kiu Enum
Kiu d liu m khi khai bo ta cn lit k ton b tr sau ny mt bin enum nhn c C php: enum ten_kieu { danh_sach_tri } bien; VD: enum weekday { sun, mon, tue, wed, thu, fri, sat } day; Khi cc tn c lit k ra tng ng vi cc gi tr int t 0 tr i

VD30: #include "conio.h #include "stdio.h" main() { enum {T0,T1,T2}; enum weekday { sun, mon, tue, wed,thu, fri, sat} day1; enum weekday day2; int i, j=2000, k = T2; i = sat; day1 = -1000; day2 = j; printf("\n day1 = %d day2 = %d i = %d",day1,day2,i); printf("\n k = %d T1 = %d",k,T1); getch(); }

nh ngha kiu bng Typedef

Typedef
T kha typedef dng t tn cho mt kiu d liu C php: typedef kieu_cu kieu_moi; VD: typedef unsigned char byte; byte a=2;

VD30: #include "conio.h #include "stdio.h" typedef double mang5_5[5][5]; typedef float mang5[5]; typedef enum {e1=3,e2,e3} e; main() { mang5 a={1,2,3,4,5}; mang5_5 b={1,2,3,4,5,6}; e t3=e3; printf("\n a[2] = %f",a[2]); printf("\n b[1][0] = %lf",b[1][0]); printf("\n t3 = %d", t3); getch(); }

VD31: #include "conio.h #include "stdio.h" typedef struct { float re, im; } sophuc; sophuc cong(sophuc,sophuc); void insp(sophuc); main() { sophuc u,v; u.re = 1.2; u.im = 3.4; v.re = 2.3; v.im = 1.5; insp(u); printf(" + "); insp(v); printf(" = "); insp(cong(u,v)); getch(); } sophuc cong(sophuc a, sophuc b) { sophuc c; c.re = a.re + b.re; c.im = a.im + b.im; return c; } void insp(sophuc i) { printf("%2.2f + %2.2lfi",i.re, i.im); }

You might also like