Royal University Of Phnom Penh Structures 1
I- Enumeration :
KWCakarkMnt;tMél member enAeBlRbkas .
Syntax : enum name {item1, item2, . . . , itemn};
Edl itemnimYy²CatMél constant EdleKGackMnt;tMéleGayva)an.
]TahrN enum weeks{sun, mon, tue, wed, thee, fri, sat};
ehIyEdlTUeTAtMél default KW³ sun=0, mon=1, . . . ebI printf(“\n%d, %d”, sun,tue); enaHeK)an Output
KW 0 , 2 .
]TahrN: enum Num{zero,one,two,three,four}; enaH zero=0; one=1;
. . . . . . ; four=4;
]TahrN: enum weeks{sun=1,mon,tue=5,wed,thu,fri=100,sat};
enaH sun=1, mon=2, tue=5, wed=6, thu=7, fri=100, sat=101;
II- typedef :
RtUv)aneKeRbIedIm,IkMnt;RbePTTinñn½yfµI(New data type)eRkABIRbePTTinñn½yEdlmanRsab; (Standard data type).
Syntax: typedef datatype newDatatype ;
]TahrN typedef int integer;
typedef char str[30];
enaHeK)anbegáItkMrUTinñn½yfµI mandUcxageRkam ³
integer i ; ( i Ca int )
str st ; ( st Ca character of array )
]TahrN: typedef int mytype;
mytype i ; int j ;
j = i +3;
III- Structures :
KWCakarRbmUlpþúMén variable EdlmanRbePTTinñn½yepSg²Kña edaybBa©ÚleGaysßiteRkamkarRKb;RKgrbs;
structure EdlmaneQµaHEtmYy .
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 2
Syntax :
Struct name{
datatype1 member1;
datatype2 member2;
. . . . . . . . . . . . .;
. . . . . . . . . . . . .;
. . . . . . . . . . . . .;
datatypen membern ;
};
rWeKGacRbkas structure:
typedef struct {
data type mumber1;
data type2 mumber2;
. . . . . . . . . . . . .;
. . . . . . . . . . . . .;
data typen mumbern;
} name;
]TahrN1: eKRbkas structure Adder edIm,IkMnt;BGI s½ydæan³
struct address {
char name[30];
int house ;
char city;
};
rWGacsresr ³
typedef struct {
char name[30];
int house;
char city[20];
}address ;
]TahrN2: karRbkas structure student edIm,IkMnt;BIlkçN³rbs;nisiStdUcxageRkam ³
struct studentType{ rW typedef struct{
char name[30]; char name[30];
char sex; char sex;
int age ; int age;
int score; int score;
char school[40]; char school[40];
char hobby[20]; char hobby[20];
}; }studentType;
sMKal; ³ karRbkas structure KWepþImeday keyword struct bnÞab;mkCaeQµaHrbs;structenaHEdleKehA
fa structure tag rWtag .
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 3
Open nig RtUv)aneKeRbICacaM)ac;.
Close brace ({...})
data type nig variable TaMgLayeKehAfa data member nig member rbs; structure enaH .
1- kar Access elI structures :
eKGac Access elI field nimYy²rbs; structure tamry³ variable én structure enaH .
]TahrN: eK Declare variable én structure kñúg]TahrNTI2 xagelIdUcxageRkam³
struct studentType stu1, stu2, stu3; rW studentType stu1, stu2, stu3;
- Edl stu1, stu2, stu3 Ca variable rbs; structure studentType .
- variable rbs; structure eRbIR)as;eTAtamFieldnimYy²Edlva accesseTA .
TMrg;TUeTAénkar Access :
[Link] = value ;
]TahrN: karAccesselI field én structure studentType tamry³ variable stu1 KW ³
strcpy( [Link] , ”sok data”) ;
[Link] = ’M’;
[Link] = 25;
[Link]=85;
strcpy( [Link], ”RUPP”) ;
strcpy( [Link], ”sport”) ;
TMrg;énkar
Access rbs;
structure Initialization: Ca
1- Struct studentType {
char name[25];
char sex ;
int Age ;
}stu = ”sok data”, ‘M’, 27};
2- struct student{
char name[25];
char sex ;
int age ;
};
struct student stu={“sok data”, ’M’, 27};
3- typedef struct{
char name[25]
char sex;
int age;
}data;
data stu ={“sok data”, ’M’, 27};
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 4
tamTMrg;énkarAccessTaMgbIxagelIenHmann½ydUcxageRkam ³
strcpy([Link], ”sok data”) ;
[Link] = ’M’;
[Link] = 27;
sMKal;³ cMeBaH string eK access edayeRbI Function strcpy( ) .
]TahrN: strcpy([Link],”sok data”) ;
enAeBlEdlkarAccess elI structure ebITinñn½yminRKb;eTAnwgField nimYy²rbs;structenaHeT enaHbNþatMél
Field esµIsnU ücMeBaHField KµantMél .
]TahrN: struct student{
char name[20];
char sex;
int age;
int score;
}stu = {“sok”, ’M’};
mann½yfa ³
[Link]=”sok”;
stu sex=’M’;
stuage=0;
stu score=0;
2- Pointer to structure :
kñúgkrNIEdl variable rbs; Structure CapionterenaH kar access elI field rbs ;struct tamTMrg;dUc
xageRkam³
(*variable).Field = value;
rW variable->Field = value;
]TahrN: struct sample{
int x ;
float y ;
};
struct sample *ptr;
(*ptr).x = 20;
(*ptr).y = 25.15;
rWGacAccessmüa:geTot ³
ptr->x = 20;
ptr->y = 25.15;
Structure Gacman data field Ca pionter :
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 5
struct sample{
int *a;
float *b;
};
struct sample *ptr;
int x=20;
float y=25.15;
ptr->a=&x;
ptr->b=&y;
Output :
printf(“a=%d”, *ptr->a); // *(ptr->a)*ptr->a
printf(“b=%f”, *ptr->b); // *(ptr->b)
]TahrN: Program sMrab; Input nig Output Data rbs; Costomer:
#include<stdio.h>
#include<conio.h>
struct customer{
char name[20];
int age;
char sex;
float *income;
};
void main( ){
clrscr();
struct customer *pt, obj;
pt=&obj;
char na[20], s;
int ag;
float in ;
printf(“input name:”); gets(na);
printf(“input sex :”); scanf(“%c”,&s);
printf(“input age:”); scanf(“%d”,&ag);
printf(“input income:”); scanf(“%f”,&in);
strcpy([Link], na);
[Link] = ag;
[Link] = s;
pt->income=∈ //[Link]=∈
puts(“output data:”);
printf(“name: %s\n”, [Link]);
printf(“age= %d\n”, [Link]);
printf(“sex= %c\n”, [Link]);
printf(“Income= %f, *pt->income);
getch();
}
ebIeKman struct customer obj, *pt;
pt=&obj; enaHeKGac Access ³
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 6
strcpy([Link], na);
rW strcpy(pt->name, na);
[Link]=ag; rW pt->age=ag); rW (*pt).age=ag;
[Link]=s; rW pt->sex=s; rW (*pt).sex=s;
3- Array of structures:
enAkñúgsßanPaBeRcIn eBlenaHeKeRbI Array of structure . eBalKWeBleFVIkarGnuvtþeTAelIRkuménmnusSeRcIn/ rW
nisSiteRcInnak; > > > > > .l.
]TahrN¾³ eKRbkas Array of structure dUcxageRkam ³
struct studentType{
char name[20];
char sex;
int age;
};
struct studentType stu[100];
- karAccesselI Array of structure :
+ Initialization: ( sMrab;nisSit3nak; )
struct studentType{
char name[20];
char sex;
int age;
}stu[3]={{“sok”,’M’,25},{“Bopha”,’F’,20},
{“Vibol”,’M’,30}};
enaHmann½yfa ³
stu[0].name=”sok”;
stu[0].sex=’M’;
stu[0].age=25;
stu[1].name=”Bopha”;
stu[1].sex=’F’;
stu[1].age=20;
stu[2].name=”Vibol”;
stu[2].sex=’M’;
stu[2].age=30;
dUcKñaenHEdr cMeBaH Array of structure ebIsinkarkMnt;tMélminRKb;tamFieldnimYy²rbs;vaeTenaH enaHtMél
FieldEdlenAsl;KWesµI 0 .
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 7
4- Structure in structures (complex structures)
eyIgsegáteXIjfa Field nimYy²rbs; structure EtgEtmanRbePTTinñn½yCak;lak;mYy . dUnenHebIsinCaman
field NamYyrbs; structure manRbePTTinñn½yCa structure NamYyepSgeTotenaH enaHeKehAfa structure in
structure rW Complex Structures .
- Declaration of structure in structure :
]TahrN¾³
struct address{
int house;
int st;
char *sangkat;
};
struct peopleType{
char name[25];
int age;
char sex;
struct address addr;
};
struct peopleType people;
rWGacsresr ³
typedef struct{
int house;
int st;
char *sangkat;
}address;
typedef struct{
char name[25];
int age;
char sex;
address addr;
}peopleType;
peopleType people;
- kar Access elI structure in structure :
tamkar declare én structure in structure xagelI eyIgGac Access dUcxageRkam ³
[Link]=(char*)malloc(25);
strcpy([Link], ”sok”);
[Link] = 27;
[Link] = ’M’;
[Link] = 119;
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 8
[Link] = 566;
[Link] = (char*)malloe(40);
strcpy([Link], ”phsar they1”);
- Initializing structure in structure :
+ tamkar
Declare structure in structure kñúgrebobTI1xagelI eyIgGacsresr ³
struct peopleType p={“sok”,27,’M’,{119,566,”phsar thmeyI”}};
+ tamkar
declare structure in structure kñúgrebobTI2 eyIgGackMnt;³
peopleType p={“sok”,27,’M’,{119,566,”phsar thmeyI”}};
5- Structures and Function :
RtUv)aneKeRbICaRbePT return type rWGaceRbICa parameterrbs; Function .
- structure
]TahrN¾³ Program bgðajnUv function edayeRbIR)as; structureEtKµan return nigKµan parameter .
#include<stdio.h>
#include<conio.h>
typedef struct{
char name[20];
char sex;
int age;
} data;
data *pt;
void getdata( ) ;
void display( );
void main( ){
getdata( );
display( );
getch( );
}
void getdata( ){
pt=(data*)malloe(sizeof(data));
printf(“Input name:”); gets((*pt).name);
printf(“input sex:”); scanf(“%c”,&(*pt).sex);
pritf(“input age:”); scanf(“%d”,&(*pt).age);
}
void display( ){
puts(“Data display”);
printf(“\n name: %s”, pt->name);
printf(“\n sex: %c”, pt->sex);
printf(“\n age: %d:”, pt->age);
}
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 9
- krNI Function man return type nigman parameter Ca structure :
edaybegáIt Function TaMgBIrxagelIeLIgvijedayeRbI return type nig parameter Ca structure .
#include<stdio.h>
#include<conio.h>
struct student{
char name[20];
char sex;
int age;
};
struct student *getdata( );
void sisplay(struct studend *);
void main( ){
struct student *get;
get=getdata( );
display(get);
getch( );
}
struct student *getdata( )
{
struct student *pt=NULL;
pt=(struct student*)malloe(struct student)
printf(“input name:”); gets((*pt).name);
printf(“input sex:”); scanf(“%c”,&(*pt).sex);
printf(“input age:”); scanf(“%d”,&(*pt).age);
return pt;
}
void display(struct student *pt)
{
printf(“\n name: %s”, get->name);
printf(“\n sex: %c”, get->sex);
printf(“\n age: %d”, get->age);
}
III- NIONS :
KWCa mYyEdlRbmUlpþúMRbePTTinñn½yepSg²Ca Unit EtmYy . ehIyEdlpÞúktMélénRbePTTinñn½yepSg²
structure
kñúg locationEtmYy . Union nwgpÞúktMélmYyénrbePTxus²Kña ehIykareRbIR)as;KWdUcCa structureEdr.
- Declaration :
union name{
datatype1 member1;
datatype2 member2;
. . . . . . . . . ;
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 10
. . . . . . . . . ;
datatypen membern;
};
]TahrN¾³
Union sample{
int first;
float second;
char third;
} one, two;
Edl one nig two Ca union variable
- kar Access elI union :
Operator RtUv)aneKeRbIrvag variable nigeQµaH field. VadUcKñanwg structure Edr dot operator RtUv)aneK
eRbIedIm,I Access eTAelI Field nimYy²rbs; union .
]TahrN¾³ tamkar declare xagelI enaHeyIgGac Access :
[Link] = 12;
[Link] = 14.54;
[Link] = ’M’
- RbsinebIeKman variable én union Ca pointer enaHeKGac Access dUc pointer to structure Edr .
]TahrN¾³ union sample *ptr;
ptr->first = 12;
ptr->second= 14.54;
ptr->third= ‘M’;
- UnionGacCa member énstructuremYy. ehIy union mYyRtUv)aneK declare Ca member én
structure mYy EtRtUvEtCa member cugeRkayeKbg¥s; .
struct value{
int slno;
char sex ;
union item{
one;
float two;
char three;
};
};
struct value *u;
- kar Access:
u->[Link] = 10;
u->[Link] = 34.55;
u->[Link] = ‘F’;
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 11
]TahrN¾³
Program bgðajBI
member union én
content nigbgðajecjnU v én union enaH ³
#include<stdio.h>
#include<conio.h>
void main( ){
union value{
int i ;
float f;
};
union vallue x;
x.i=10;
x.f=-1456.45;
printf(“first member= %d\n”, x.i);
printf(“second member= %f\n”, x.f);
getch();
}
output :
First member= 3686 // Error
Second member = -1456.45;
Program xagelI Union man2member ¬Ca int nigCa float ¦ . manEttMél float mYyeTRtUv)anpÞúk
nige)aHecj cMENkÉtMél int KWe)aHminRtwmRtUv . Union pÞúktMélEtmYysMrab; Data type mYyEdltMrUvkarpÞúkFMCageK
kñúgcMeNam member rbs;va .
Programe bgðajBI
member union én
structure Ca ehIybgðajBI content én union enH
#include<stdio.h>
#include<conio.h>
void main(){
struct data{
int day;
int month;
int year;
};
union value{
int I;
floatf;
struct data bdate;
};
union value x;
x.i = 10;
x.f =-1456.45;
[Link] = 12;
[Link] = 7;
[Link] = 1995;
printf(“First member= %d\n”, x.i);
printf(“second member= %f/n”, x.f);
By Lecturer Thap Boung
Royal University Of Phnom Penh Structures 12
printf(“%d/ %d/ %d \n”, [Link], [Link],
[Link] );
}
Output:
first member =12
second member =0
12/ 7/ 1995
Program bgðajB uI nion mYyCa pionter ehIybgðajBI content én union enH edayeRbI pionter
#include<stdio.h>
#include<conio.h>
void main()
{
union value{
int i;
float f;
};
union value *a;
a->i = 10;
a->f = -1456.45;
printf(“First member= %d\n”, a->i);
printf(“second member= %f\n”, a->f);
}
Output :
First member = 3 686
second membe r= -1456.45
By Lecturer Thap Boung