L09 struct keyword

Page 1

emeronTI9 ³

sagsg;TMrg;Tinñn½yCamYynwg Struct Keyword mkdl;eBlenH GñkR)akdCa)andwgy:agc,as;ehIyGMBIrebobRbkasGefr sMrab;pÞúk RKb;RbePTTinñn½y dUcCaRbePTcMnYnKt; (Integer) cMnYnTsPaK (Floating -point) nigRbePTtYGkSr CaedIm (Character). minEtb:ueNÑaHGñkk¾)andwgGMBvI Fi bI egátI es‘réI ncMnYnTaMgenaH (Array) niges‘rIén Pointer Edlcg¥úleTAkan;TItaMgNamYy én Memory EdlmanpÞk ú Tinñn½yRbePTNamYy EdleyIgcg;)an. ]bmafaGñkRtUvkar sresrkmμvFi mI YysMrab;RKb;RKgBt’manrbs;buKlÁ ki Rkumh‘un {k} ehIyBt’manEdl GñkRtUvkarkt;RtaBIbuKÁlikenaHrYmmandUcCa eQμaH GtþelxkñúgRkumh‘un éf¶ExqñakM eM NIt Gasydæansñak;enAbc©úb,nñCaedIm EdlkñúgenaHGñk)aneXIjfamanBt’manxøHman RbePTCa String nigxøHeTotCatMélelx. CakarBitNas; eBlenHedIm,IrkSaTuk Tinñn½yrbs; buKÁlik TaMgenaH KWGñkGacbegáIt Array sMrab;RbePTTinñn½ynImYy² nig rkSaTukBt’manTaMgenaH )any:aggay k¾bu:EnþeFVItamlkçN³enHKWmanPaBceg¥ótcg¥l; Nas; BIeRBaHvamin)anGnuBaØat eGayGñkGac cUleTAdl;karBinitüemIlBt’manGMBI Gasydæansñak;enArbs;buKÁlikeQμaH {sm} b¤k¾Bt’manGMBIéf¶ExqñaMkMeNItrbs; kBaØaeQμaH {K} )anedaygayRsYlenaHeT KWRtUvkarGanBIRKb; Array EdlmanTMnak; TMngCamYyBt’manEdleyIgRtUvkar CamYynwgelxlMdab; (Index) NamYyy:agRtwm RtUv. k¾bu:Enþ/ kñúgPasa C )anGnuBaØateGayeyIgmanlT§PaBGaceFVIkarCamYyTMrg;


kargarRbePTenHedayviFmI yY epSgeTotEdlmanlkçN³RbesICagkareRbI Array dUc Edl)anerobrab; munenH enaHKWkarerobcMTMrg;Tinñn½yfμImYyedayBaküKnøwH struct. I. Definition Structure :

eyIg)ansÁal;bNþaTinñn½ymanTMrg;xøH²dUcCa Array, EdlmanbNþaFatu manRbePTTinñn½ydUcKña . Ex: int x[100]; b:uEnþebIeyIgcg;begáItTinñn½ymanTMrg;/ EdlbNþaFaturbs;vamanRbePTxusKñaenaHeyIgRtUv Declaration vaCaTinnñ y ½ RbePT struct . ehIyGacehATinñn½yRbePT struct fa record k¾)an . II. Declaration Struct:

Tinñn½yRbePT structure RtUv Declaration eday Keyword struct, bnÞab;mkCabNþa field rbs; struct . Ex1:

rYmman elxpÞH/ eQμaHpøÚv/ TIRkug/ dUecñHeyIgGac Declaration Address CamYy keyword struct dUcxageRkam ³ Address

struct Address { int home, char street[20]; char city[15]; } A, B;

b¤mü:ageTotmin)ac;maneQñaHrbs; ; struct eT/ KW Declaration edaypÞal ;bNþa Variable A, B dUcxageRkam ³ struct /* No Name */ { int home; char street [20]; char city[15]; } A, B;

dUecñHeyIgman field bIKW Home, Street, City EdlmanRbePTTinñn½yepSg²Kña ehIymanTMnak; TMngCamYyKñabegáIt)an Address . eRkaymkeyIg Declaration Variable A, B Ca struct . ª eyIgGac Declaration struct nig Variable dak;epSgBIKña)an . Note:

struct Address

190


{

int home; char street[20]; char city[15];

}; Declaration Variable : struct Address A, B; Ex2:

edIm,I Declaration ry³eBl DATE eyIgman 3field: éf¶/ Ex/ qñaM .

struct Date { unsigned char day;

unsigned char month; unsigned int year; } date1, deta2;

b¤ struct Date date1, date2 edayéf¶/ Ex/ qñaM KñacMnYnGviC¢man dUecñHeyIg Declaration Ca unsigned . Ex3: Declaration RbvtþirUbrbs;buKÁlikmñak;rYmman ³ eQμaH/ éf¶ExqñaMkMeNIt/ Gas½ydæan/ R)ak;Ex >>> struct Date { unsigned char day; unsigned char month; unsigned int year; }; struct people { char dat; struct Date Dob; unsigned char sex; struct Address addre; float salary; } peopleA, peopleB; -

/*girl = 0, boy = 1*/

kñúg Ex3, eyIgsegáteXIjfa Declaration struct kñúg struct . ebIcg;tagtMéleGayFatu Name eyIgsresr ³ peopleA.Name = “Dara”;

-

ebIcg;tagtMéleGayFatu City KWsresr ³ peopleA.Address.City = “Phnom Penh”;

III. Access to Faturbs; struct:

CamYy Declaration : struct Address { int home;

191


char street[20]; char city[15]; } A, B;

eyIgGac Access eTAFaturbs; struct dUcxageRkam³ A.home = 118; A.street = “sihanu”; A.city = “Phnom Penh”;

b¤eRbIkñúg Function: printf (“Enter Name Street:”); gets (A.street);

IV. Declaration Array of struct:

eyIgGac declaration array of struct edIm,IpÞúkTinñn½y)aneRcIn Ex: struct people a[100];

Cakar declaration Array 100, a manRbePTCa people . k¾dUcCabNþa epSgeTotEdr/array of struct k¾RtUvman index cab;BeI lx 0 eTAEdr . Binti ü]TahrN_xageRkam³ date { char Name[80]; int day; int month; int year; } static struct date dateofbirth [ ] = {

array

Ex:5

b¤ static struct date dateofbirth[5];

“Data”, 10, 3, 1970, “Bopha”, 22, 4, 1968, “Vanna”, 11, 11, 1950 };

deteofbirth[2].Name = “Vanna”; V.

]TahrN_Gnuvtþn_ ³

]TahrN_xageRkamenHGnuvtþn_nUvGVI²EdleyIg)aneronknøgmkdUcCa

function… # include <stdio.h> # include <conio.h>

192

array,

struct,


# define MAX 100 struct Address { char name[30]; int homenumber; char street [20]; char city[15]; } addr[MAX]; void init_list (void); void enter (void); int menu_select (void); void delete1 (void); void list (void); int find_free (void); main ( ) { char choice; init_list ( ); /* create array of struct */ for (, ,) /* loop have no condition */ { choice = menu_select ( ); switch (choice) {case 1: enter( ); break; case 2: delete1 ( ); break; case 3: list ( ); break; case 4: exit (0); } } }

edIm,IbegátI tMélrbs;

function init_list array Address /* create list of Address */ void init_list (void) { register int t; for (t = 0 ; t < MAX ; ++t) addr[t].name[0] = ‘\0’; } /* create menu selete*/ int menu_select (void) { char s[80] ; int c ; printf (“1. Enter Name \n”); printf (“2. Delete Name \n”); printf (“3. List Name \n “); printf (“4. Exit \n”); do {

193

.


printf (“\n Please select your choose:”); gets(s); c = atoi(s); } while (c< 0 || c > 4); return (c); } /*Enter Address into list */ void enter (void) { int slot ; char s[80] ; /* find free space in array for access */ slot = find_free ( ); if (slot = = -1) { printf (“\n list is full !”); return ; } printf (“Name :”); fflush(stdin); gets (addr[solt].name); printf (“Home Number :”); scanf (“%d”, &addr[solt].homenmber) while (gecthar ( )!=’\n’; prinft (“Name street :”); gets(addr[solt].street); printf(“ City :) ; fflush(stdin); gets(addr[slot].city); } /*find free space */ int find_free (void) { register int t; for(t = 0;addr[t].name[0] && t<MAX;t++) if (t = =MAX) return (-1); /*No free space*/ return(t); }

cMNaM ³ eyIgeRbItéM l -1 edIm,IR)ab;faGs; eRbIR)as;Ca Index.

/*Delete*/ void delete1 (void) { register int slot; char s[80];

194

space

. tMél-1 CatMélEdl

array

min


printf (“´Number of Record # :”); gets(s); slot = atoi (s); if (solt >= 0&& slot <MAX) addr[slot-1].name[0]= ‘\0’; } /* Display Data On Screen */ void list (void) { register int t; for (t = 0; t <MAX ; ++t) { if (addr[t].name[0]) { printf (“%s \n”, addr[t].name); printf (“%d \n”, addr[t].homenumber); printf (“%s \n, addr[t].street); printf (“%s \n, addr[t].city); } } printf (“\n\n”); }

kñúg function enter( ), eyIgeQIjfa scanf (“%d”, &addr[slot].homenumber); while (getchar( ) !=’\n’);

eBleyIg Read pÞHelx, eyIgRtUveRbIsBaØa Address & edIm,I Read . cMnucmYyeTot eyIgRtUveRbI statement while (getchar( ) !=’\n’); edIm,IrMlgsBaØa Enter ehIyeyIgcuHedImbnÞat;fIμ . ebIKμan statement enH, enaH statement Read mþgeTotnwgxus. ebIeyIglub statement while (gechar( ) !\n’); enaHeBleyIg Read eQμaH street nwgKμanGIVTaMgGs;. VI.

bBaöÚl

parameter eRkamerog struct eTAeGay function :

eBlbBaöÚl parameter RbePT struct eTA function, eyIgGacbBaöÚnbNþa nImYy²rbs; struct eTAeGay struct . ]TahrN¾ xageRkamnwgGFib,ayBIbBaÐaenH. struct toto { char x; int y; float z; char s[10];

195

filed


}tata;

nImYy²rbs;

field struct func1 (tata.x); func2 (tata.y);

nwgRtUvbBaöÚneTAeGay function dUcxageRkam :

bBaöÚn Address rbs; string s*/ func4 (tata.s[2]); /*bBaöÚn value character */ /* eyIgGacbBaöÚn Address rbs;bNÞa field eTAeGay function func3 (tata.s);

/*

func1 (&tata.x); func2 (&tata.y); func3 (tata.s) func4 (&tata.s[2]); +

bBaöÚn Address rbs; string*/ /*bBaöÚn character*/

/*

bBaöÚn parameter Ca struct : Ex7: #include <stdio.h> struct type_toto {

int a, b; }; void f1 (struct type_toto parm); main ( ) { struct type_toto arg; arg. a= 1000; f1 (arg); } void f1 (struct type_toto parm) { printf(“%d”, parm.a); }

VII. Pointer of stuct :

kñúgCGnuBaØatieGayeRbI Pointer point to struct )an. Ex: struct bal { char name[80] ; } person ;

196

*/


strcut bal *p;

KWCa Declaration pointer RbePT bal (struct),. EtminTan;man Address Cak;EsþgeT . eyIgtag p = &person ;

eBlenH p pÞúk Address CamYyFaturbs; struct tamry³ pointer point to vaenH/ eyIgeRbIR)as; operator EdlmansBaØa Æ. ]bmancg; point to field balance,eyIgRtUvsresr³ p Æbalance; Note: cMeBaHkar Access CamYyFaturbs; struct tamry³ pointer enaHeyIgeRbIsBaØa Æ . cMBaH access CamYyFaturbs; struct tamry³ variable FmμtaenaHeyIgeRbIsBaØa (.) . Ex8: GFib,ayBInaLikar ³ # include <stdio.h> # define DELAY 128000 struct my_time { int hours; int minutes; int seconds; int hunds; }; void display (struct my_time *t); void update (struct my_time *t); void delay (void); main ( ) { struct my_time systime ; systime.hours = 0; systime.minutes = 0; systime.seconds = 0; systime.hunds = 0; textmode(C40); textbackground(1); textcolor(14);clrscr (); for (; ;) /* loop have no condition */ { update (&systime); display (&systime); } return 0; } void display (struct my_time *t) {gotoxy(18, 12 ); cprintf (“%02d :”, tÆhours); gotoxy(21,12); cprintf (“%02d :”, tÆminutes); gotoxy(24,12) ; cprintf (“%02d :”, tÆseconds); gotoxy(27,12); cprintf (“%02d ”, tÆhunds); }

197


void update (struct my_time *t) { tÆhunds ++; if (tÆhunds = = 100) { tÆhunds = 0; t Æseconds ++; } if (tÆseconds = = 60) { tÆseconds = 0; t Æminutes ++; } if ( tÆminutes = =60) { tÆminutes = = 0; tÆhours ++; } if ( tÆhours = = 24) tÆhours = 0 ; delay (); } void delay (void) { long int t; for (t = 1; t < DELAY; ++t); }

lTæpldUcxageRkam ³

Note :

14 : 02 : 55 : 50 -

-

eyIgGacERbRbYltMélrbs ; DELAY edIm,IkmμvFi tI eM NIrkaryWt² kñúgEpñkxagedIménkmμvFi /I eyIg Déclaration struct my_time EtminTan;man variable Cak;EsþgeT . kñúgEpñk main( ) eyIg Declaration systime EdlmantMéledIm 00: 00:00 . mann½yfa systime, dMeNIrkarEtkñúg main ( ) Etb :ueNÑaH . Function update ( ) eFVIeGayERbRbYltamry³eBl nig function display ( ) eRbIsMrab; display result on screen . kñúg Function TaMgBIrsuTæEtman parameter Ca pointer point to my_time . In Function update() nig display(), Fatu nImYy²rbs; systime RtUv)an Access tamry³ pointer . edayvaGac update tamélry³eBl . 198


VIII. union:

ebIeyIg Declaration variable eRcInCa Function enaHvaeRbIEt block memory EtmYyb:ueNÑaH/ eTaHCaenAry³eBlxusKñak¾eday ³ ]TahrN_ union

union table {

int number; long address; char code;

}; union table a; union table b; union table c;

dUecñH Declaration Ca union eFViIeGaymankarsnSMsMéc memory .

=====*=====

199


lMhat;Gnuvtþn_ 1.

1.1.

cUrsresrkmμvIFIGnuvtþn_xageRkam a. Read form Keyboard nUv Data én array of struct EdlmanRbePTCa point dUcxageRkam ³ struct point { int num ; float x ; float y ; }

cMnYnFatuén array RtUvkMNt ;eday statement # define b. Display the Result on screen.

sresrkmμvIFIxagelIeRkamrag Function : Funtion mYysMrab; Read Data, Function mYyeTotsMrab; Display . 2. sMnYrbEnßm ³ 1.2.

union table a; 2.1.

cUrrkcMnucxuskñúg struct xageRkam ³ struct parts { int partno; char descript[20]; float price; char *ptr;

2.2.

eFVIdUcemþcedIm,IsMEdgFatu partno rbs; parts.

struct inven { int partno ; } /* struct inven parts; */ struct inven parts; struct inven *ptr = &parts; a. part.partno b. ptrÆpartno c. partno d. a b

nig

200


2.3.

cUrbegáIteQμaHmYyeGay character TI3 én variable name rbs; address . struct fullname{ char firstname[20]; char lastname[20];

}; struct addr { struct fullname name ; char address[60]; char city[4]; } struct fullname { char firstname[20]; char lastname[20]; } struct addr address; struct addr *ptr = &address; a. address.namefirstname[2]; b. ptrÆname.firstname[2]; c. a, b

cMelIy suTæEtRtUv d. KμancMelIyRtwmRtUveT .

=====*=====

201


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.