Module 7 Working with Structures Structure Definition struct

  • Slides: 20
Download presentation
Module 7 Working with Structures

Module 7 Working with Structures

Structure Definition struct tag { member 1_declaration; member 2_declaration; member 3_declaration; . . .

Structure Definition struct tag { member 1_declaration; member 2_declaration; member 3_declaration; . . . member. N_declaration; }; www. umbctraining. com @Copyright UMBC Training Centers 2012 2

struct point { int x; int y; }; struct point p 1, p 2;

struct point { int x; int y; }; struct point p 1, p 2; p 1. x = 4; p 1. y = 2; p 2. x = p 1. x * 2; p 2. y = p 1. y + 4; printf(“p 1 = (%d, %d)n”, p 1. x, p 1. y); www. umbctraining. com @Copyright UMBC Training Centers 2012 3

Let’s take a look • CB – Struct. Initialization • CB - Struct. Alignment

Let’s take a look • CB – Struct. Initialization • CB - Struct. Alignment www. umbctraining. com @Copyright UMBC Training Centers 2012 4

typedef • Gives an alternative name to an existing typedef double LENGTH; • Especially

typedef • Gives an alternative name to an existing typedef double LENGTH; • Especially common with structs typedef struct point { int x; int y; }POINT; www. umbctraining. com @Copyright UMBC Training Centers 2012 5

struct parameters • A struct may used as a function parameter passed by value

struct parameters • A struct may used as a function parameter passed by value void print. Point(struct point p) { printf(“(%d, %d)”, p. x, p. y); } // OR IF POINT TYpe. Defed void print. Point(POINT any. Pt) { printf(“(%d, %d)”, any. Pt. x, any. Pt. y); } www. umbctraining. com @Copyright UMBC Training Centers 2012 6

Struct parameters Non-typedefed struct point center, corner; center. x = 4; center. y =

Struct parameters Non-typedefed struct point center, corner; center. x = 4; center. y = 9; corner. x = 7; corner. y = 12; print. Point( center ); print. Point( corner ); struct point center; POINT corner; center. x = 4; center. y = 9; corner. x = 7; corner. y = 12; print. Point( center ); print. Point( corner ); (both of these in main() ) www. umbctraining. com @Copyright UMBC Training Centers 2012 7

struct as Function Return struct point POINT input. Point( ) { struct point p;

struct as Function Return struct point POINT input. Point( ) { struct point p; printf(“Input coordinates: ”); scanf(“%d %d”, &p. x, &p. y); return p; } In main: struct point POINT center, end. Point; center = input. Point( ); end. Point = input. Point( ); www. umbctraining. com @Copyright UMBC Training Centers 2012 8

Initializing a struct • A struct may be initialized by providing an appropriate initial

Initializing a struct • A struct may be initialized by providing an appropriate initial value for each data member struct point{ int x, y; }; struct point p = {43, 17}; struct point c = {. y = 4}; • A function to create an initialized (but “empty”) struct is a good idea struct point new. Point( ) { struct point p; // p = {0, 0}; would work too p. x = 0; p. y = 0; return p; } struct point center = new. Point(); www. umbctraining. com @Copyright UMBC Training Centers 2012 9

Let’s take a look • CB—Program 9 -4 and 9 -5 www. umbctraining. com

Let’s take a look • CB—Program 9 -4 and 9 -5 www. umbctraining. com @Copyright UMBC Training Centers 2012 10

struct Assignment struct point p 1; struct point p 2; p 1. x =

struct Assignment struct point p 1; struct point p 2; p 1. x = 44; p 1. y = 56; /* assignment copies members */ p 2 = p 1; www. umbctraining. com @Copyright UMBC Training Centers 2012 11

Exercises • Pg 191 • #1 (1 or 2) – type in programs from

Exercises • Pg 191 • #1 (1 or 2) – type in programs from text • #2 – date. Diff (note textbook typos) – In the example, the date given doesn't match (day number is off) • In N 1 , + 3 should be + 8 (day) • #4 – extension of #2 • #5 – combining update. Time and update. Date from 9. 4 and 9. 5 www. umbctraining. com @Copyright UMBC Training Centers 2012 12

Array of structs struct point POINT random. Points[5]; /* the y-coordinate of the 2

Array of structs struct point POINT random. Points[5]; /* the y-coordinate of the 2 nd point random. Points[1]. y = 33; /* the x-coordinate of the 3 rd point */ int point 3 x = random. Points[2]. x; www. umbctraining. com @Copyright UMBC Training Centers 2012 13

Nested structs typedef struct line { struct point left. End; struct point right. End;

Nested structs typedef struct line { struct point left. End; struct point right. End; }; LINE diagonal; // using typedef /* the x-coordinate of diagonal’s left. End */ int left. End. X = diagonal. left. End. x; www. umbctraining. com @Copyright UMBC Training Centers 2012 14

Initializing Nested structs • Provide initial values for each member and for each member

Initializing Nested structs • Provide initial values for each member and for each member of each nested struct LINE center. Line = { {4 , 5}, /* left endpoint */ {6, 7}, /* right endpoint */ 3 /* width. In. Pixels */ }; www. umbctraining. com @Copyright UMBC Training Centers 2012 15

Let’s take a look • CB – struct. Truck – truck has engine, radio,

Let’s take a look • CB – struct. Truck – truck has engine, radio, other attributes – Create this www. umbctraining. com @Copyright UMBC Training Centers 2012 16

structs Containing An Array struct student. Grades { int student. ID; int grades[10]; };

structs Containing An Array struct student. Grades { int student. ID; int grades[10]; }; struct student. Grades bobs. Grades; bobs. Grades. student. ID = 12345; for(int i = 0; i < 10; i++) bobs. Grades. grades[i] = 0; bobs. Grades. grades[0] = 55; bobs. Grades. grades[1] = 99; www. umbctraining. com @Copyright UMBC Training Centers 2012 17

Let’s take a look • CB – struct. House has an array of Windows

Let’s take a look • CB – struct. House has an array of Windows – Char arrays common – struct month (text pg 187) – Initialization – Array of months… accessing each element – diagram on the board -- animated • months[0]. nr. Days = 31 days in Jan • months[2]. name[1] = ‘a’, from “Mar” • CB – Blocks Structs www. umbctraining. com @Copyright UMBC Training Centers 2012 18

Exercises • Ex 1 – Ex 5 www. umbctraining. com @Copyright UMBC Training Centers

Exercises • Ex 1 – Ex 5 www. umbctraining. com @Copyright UMBC Training Centers 2012 19

www. umbctraining. com @Copyright UMBC Training Centers 2012 20

www. umbctraining. com @Copyright UMBC Training Centers 2012 20