lecture 16 cis 208 friday march 18 th, 2005. last bit on structs, i swear structs in debugger it’s...

18
Lecture 16 CIS 208 Friday March 18 th , 2005

Upload: gabriel-neal

Post on 24-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Lecture 16

CIS 208Friday March 18th, 2005

Page 2: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Last bit on structs, I swear

Structs in Debugger

It’s your friend

Page 3: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Enumerations

new types with limited, specific values

Example: Create a coin type that only allows the values:

penny, nickel, dime, quarter, dollar

called symbols

Page 4: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Money type

enum coin {penny,nickel,dime,quarter,dollar};

//type definitionenum coin money; // variable declaration

value money now will only accept values called penny, nickel, etc.

Page 5: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

enums

money = dime;money = penny;if(money == dime) printf(“dime”);

penny, dime,etc are now values, like 1, ‘a’, 2.3

Page 6: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

enums

actual values of penny, nickel, etc are ints.penny == 0;nickel == 1;dime == 2; etc

given values in order of definition

enum coin{penny, nickel, dime…}; 0 1 2

Page 7: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

values of enums

Since values are just ints, they act like ints

printf(“%d %d”, penny, dime);0 2

Using enumerations let’s users not care about actual values Especially if the enumeration is defined in a

header file

Page 8: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Initial values

Enums don’t have to use default values.enum coin{penny,nickel, dime, quarter = 100,

half-dollar, dollar};

penny = 0nickel = 1dime = 2quarter = 100half-dollar = 101dollar = 102

Page 9: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

enums values

Don’t have to be unique

enum foo{fred =1 , lucy = 1, ethel = 1};

Page 10: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

using enums

switch(money) {case penny: value += 1;break;case nickel: value +=5;break;case dime: value += 10;

break;etc

Page 11: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

one last example

char name[][12] = {“penny”,“nickel”,“dime”,“half-dollar”,“dollar”};

printf(“%s”, name[money]);

only works if no symbol has been initialized.

Page 12: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Unions

Memory location shared by two or more different variables

Usually of different types, at different times.

Generally just one at a time

Page 13: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

Unions

declared like structsunion u_type {

int j;char ch;

};

union u_type bar;

Page 14: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

unions

all variables exist in same memoryint j & char ch share some memory.

changing one might change the other.

Page 15: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

access

same as structs,

. operator for actual union variables-> operator for pointers to unions

Page 16: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

allocation

default size is the size of the largest element.

for dynamic allocation, use sizeof

Page 17: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

‘practical’ example

struct symbol{enum type {INT, CHAR, STRING} kind;

union foo { int I;

char C;char S[10];} val;

} mySym;

Page 18: Lecture 16 CIS 208 Friday March 18 th, 2005. Last bit on structs, I swear Structs in Debugger It’s your friend

continued

switch (mySym.kind) {case INT: printf(“%d”, kind.val.I);break;case CHAR: printf(“%c”, kind.val.C);break;case STRING: printf(“%s”,kind.val.S);

}