Machine-Level Programming 6 Structured Data Topics Structs Unions.
Structs within structs
description
Transcript of Structs within structs
2007 Pearson Education, Inc. All rights reserved.
Structs within structs
typedef struct{
short int year;
unsigned int month; /* 1..12 */
unsigned int day; /* 1..31 */
}DATE;
DATE birthday = {1963, 1, 4};
1
2007 Pearson Education, Inc. All rights reserved.
if( birthday.year < 2000 ){
printf(" Born last century!\n”);
}
2
2007 Pearson Education, Inc. All rights reserved.
typedef struct{
short int year;
unsigned char month; /* 1..12 */
unsigned char day; /* 1..31 */
}DATE;
typedef struct{
DATE birthday;
char gnd;
float weight;
}FRIEND;
FRIEND esam;
FRIEND zayed ={ {1990,12,31}, ’M’, 61.5}
/*FRIEND zayed ={ 1990,12,31, ’M’, 61.5}*/
3
2007 Pearson Education, Inc. All rights reserved.
printf( "Date of birth is %2u/%2u/%4u \n“,
zayed.birthday.day, zayed.birthday.month,
zayed.birthday.year );
%d decimal (29, -12)
%u unsigned value (29, 12)
4
2007 Pearson Education, Inc. All rights reserved.
Create a struct Appointment– First what is an appointment
5
2007 Pearson Education, Inc. All rights reserved.
Appointment = Date + Time + description
What is Date and what is Time
6
2007 Pearson Education, Inc. All rights reserved.
Date = year + month + day
Time = hour + minuets + seconds
7
2007 Pearson Education, Inc. All rights reserved.
typedef struct Date{
int y,m,d;
} DATE;
typedef struct Time{
int h,m,s;
} TIME;
typedef struct appointment{
DATE d;
TIME t;
char description[100];
} APPOINTMENT;
create a variable of APPOINTMENT with initial values
8
2007 Pearson Education, Inc. All rights reserved.
APPOINMENT review = {
{2010, 11, 28},
{5 , 30, 0},
“to review programming”
};
APPOINMENT review = {2010, 11, 28, 5 , 30, 0,“to review programming”};
9
2007 Pearson Education, Inc. All rights reserved.
Create a struct name it circle which has a point
10
2007 Pearson Education, Inc. All rights reserved.
Create a struct name it circle which has a point– First what is a circle
11
2007 Pearson Education, Inc. All rights reserved.
Create a struct name it circle which has a point– First what is a circle
– Circle = ( center ) point + radius
– What is a point
12
2007 Pearson Education, Inc. All rights reserved.
Create a struct name it circle which has a point– First what is a circle
– Circle = point + radius
– What is a point
– Point = x-axis + y-axis
13
2007 Pearson Education, Inc. All rights reserved.
typedef struct { int, x,y; } POINT;
typedef struct{ POINT centre; double rds;} CIRCLE;
Typedef struct{
int x,y;
}POINT;
Typedef struct{
POINT centre;
double rds;
}CIRCLE;
/*create a point variable */
14
2007 Pearson Education, Inc. All rights reserved.
POINT startpoint;
POINT startpoint = {10,10};
create a circle its centre is the original point
15
2007 Pearson Education, Inc. All rights reserved.
CIRCLE crl1 = {0,0, 3.5};
Create a circle and then assign its center startpoint and 2.4 to the radius
16
2007 Pearson Education, Inc. All rights reserved.
CIRCLE crl2 ;
crl2.c.x = startpoint.x;
crl2.c.y= startpoint.y;
crl2.rds = 2.4;
crl2.c = startpoint;
crl2.rds=2.4;
17
2007 Pearson Education, Inc. All rights reserved.
Create a struct name it person where each person has a name, address, and birth day
18
2007 Pearson Education, Inc. All rights reserved.
typedef struct {
char first[20];
char father[20];
char family[20];
}NAME;
typedef struct{
char country[20];
char city[20];
char street[2];
int bulding;
int flat;
} ADDRESS;
19
typedef struct{
int y,m,d;
}DATE;
typedef struct {
NAME pname;
ADDRESS padd;
DATE pbd;
}PERSON;
2007 Pearson Education, Inc. All rights reserved.
ASS
1 - Create a variable from each with initial values 2 - Create a variable from PERSON and then
show how this variable can be assigned values from the variables created at 1
20
2007 Pearson Education, Inc. All rights reserved.
ASS
Recreate the STUDENT struct with a birth day member and then create two variables one initial values and the second allow the user to input the values
21