Structs within structs

21
2007 Pearson Education, Inc. All rights rese 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

description

typedef struct{ short int year; unsigned int month; /* 1..12 */ unsigned int day; /* 1..31 */ }DATE; DATE birthday = {1963, 1, 4};. Structs within structs. if( birthday .year < 2000 ){ printf(" Born last century !\ n”); }. typedef struct{ short int year; - PowerPoint PPT Presentation

Transcript of Structs within structs

Page 1: 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

Page 2: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

if( birthday.year < 2000 ){

printf(" Born last century!\n”);

}

2

Page 3: Structs within structs

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

Page 4: Structs within structs

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

Page 5: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

Create a struct Appointment– First what is an appointment

5

Page 6: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

Appointment = Date + Time + description

What is Date and what is Time

6

Page 7: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

Date = year + month + day

Time = hour + minuets + seconds

7

Page 8: Structs within structs

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

Page 9: Structs within structs

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

Page 10: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

Create a struct name it circle which has a point

10

Page 11: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

Create a struct name it circle which has a point– First what is a circle

11

Page 12: Structs within structs

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

Page 13: Structs within structs

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

Page 14: Structs within structs

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

Page 15: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

POINT startpoint;

POINT startpoint = {10,10};

create a circle its centre is the original point

15

Page 16: Structs within structs

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

Page 17: Structs within structs

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

Page 18: Structs within structs

2007 Pearson Education, Inc. All rights reserved.

Create a struct name it person where each person has a name, address, and birth day

18

Page 19: Structs within structs

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;

Page 20: Structs within structs

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

Page 21: Structs within structs

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