enum and struct - milwaukee school of engineering...ee 1910 4 © tj enumerated types •enum...

29
Enum and Struct Last updated 10/30/18

Upload: others

Post on 21-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

Enum and Struct

Last updated 10/30/18

Page 2: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

2 © tjEE 1910

Type Definition

• C Types

Derived

Function Array Pointer Structure Union Enumerated

Page 3: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

3 © tjEE 1910

Type Definition

• Typedef

• Define a new Type

• Inherits members and operations from a standard or previously defined derived type

• Typically done in global area so all parts of the program will recognize it

typedef type IDENTIFIER;

typedef int AGE; // define a new type called AGE

// that acts like an int

Page 4: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

4 © tjEE 1910

Enumerated Types

• Enum

• Declare a new Type

• Define its name and its members (enumerate them)

• Members are mapped to integer values• Normally 0 - n

enum typeName {idenitifier list};

enum wireColor {RED, BLUE, BLACK, WHITE};

Page 5: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

5 © tjEE 1910

Enumerated Types

• Enum

• Create variables

enum wireColor power;

enum wireColor gnd;

enum wirecolor signal;

• power, gnd, and signal can only have 4 values (RED, BLUE, BLACK, WHITE)

Page 6: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

6 © tjEE 1910

Enumerated Types

• Enum

• Assign values

power = BLACK;

gnd = WHITE;

signal = RED;

enum wireColor input;

input = signal; // input now RED

Page 7: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

7 © tjEE 1910

Enumerated Types

• Enum

• operations• Enumerated types are integers

• All integer operations can be applied to an enumerated type

• No checking is done to ensure the result is valid

enum month {JAN, FEB, MAR, … NOV, DEC};

enum month birthMonth; // create a variable of

// type month

if (birthMonth == MAY){

Page 8: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

8 © tjEE 1910

Enumerated Types

• Enum

enum month {JAN, FEB, MAR, NOV, DEC};enum month birthMonth;enum month currentMonth;

if (birthMonth > currentMonth){…

switch(currentMonth){case JAN: // case 0

…case FEB: // case 1

0 1 2 10 11

Page 9: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

9 © tjEE 1910

Enumerated Types

• Enum

enum month {JAN, FEB, MAR, NOV, DEC};

• suppose we’d like the member numbers to match some other pattern

enum month {JAN=1, FEB, MAR, NOV, DEC};

0 1 2 10 11

1 2 3 11 12

Page 10: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

10 © tjEE 1910

Enumerated Types

• Enum

• Anonymous Enumeration

enum {OFF, ON}; // assign OFF the value 0, ON: 1

enum {SPACE = ‘ ‘, COMMA = ‘,’ , COLON = ‘:’};

Page 11: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

11 © tjEE 1910

Enumerated Types/** enum_types.c** Created on: Feb 8, 2018* Author: johnsontimoj*/// examples of enumerated types//#include <stdio.h>

// define type and declare variables// in global area so all parts of the// program can see themenum wire_color {RED, WHITE, BLUE, BLACK};enum wire_color gnd;enum wire_color vcc;enum wire_color sig;

int main(void){setbuf(stdout, NULL);

// initialize variablesgnd = WHITE;vcc = BLACK;sig = RED;

printf("gnd value is %i\n", gnd);printf("vcc value is %i\n", vcc);printf("sig value is %i\n", sig);

enum wire_color tmp;tmp = sig;printf("tmp value is %i\n", tmp);

enum month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};enum month birth_month;

birth_month = JUL;printf("birth month is %i\n", birth_month);

birth_month++;printf("birth month is %i\n", birth_month);

if(birth_month > APR)printf("birth month is after april\n");

elseprintf("birth month is before or equal to april\n");

birth_month -= 3;printf("birth month is %i\n", birth_month);

birth_month = birth_month << 3;printf("birth month is %i\n", birth_month);

const char* month_name[] = {"err", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};

birth_month = 5;printf("birth month is %s\n", month_name[birth_month]);

return 0;} // end main

Page 12: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

12 © tjEE 1910

Enumerated Types/** enum_types.c** Created on: Feb 8, 2018* Author: johnsontimoj*/// examples of enumerated types//#include <stdio.h>

// define type and declare variables// in global area so all parts of the// program can see themenum wire_color {RED, WHITE, BLUE, BLACK};enum wire_color gnd;enum wire_color vcc;enum wire_color sig;

int main(void){setbuf(stdout, NULL);

// initialize variablesgnd = WHITE;vcc = BLACK;sig = RED;

printf("gnd value is %i\n", gnd);printf("vcc value is %i\n", vcc);printf("sig value is %i\n", sig);

enum wire_color tmp;tmp = sig;printf("tmp value is %i\n", tmp);

enum month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};enum month birth_month;

birth_month = JUL;printf("birth month is %i\n", birth_month);

birth_month++;printf("birth month is %i\n", birth_month);

if(birth_month > APR)printf("birth month is after april\n");

elseprintf("birth month is before or equal to april\n");

birth_month -= 3;printf("birth month is %i\n", birth_month);

birth_month = birth_month << 3;printf("birth month is %i\n", birth_month);

const char* month_name[] = {"err", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};

birth_month = 5;printf("birth month is %s\n", month_name[birth_month]);

return 0;} // end main

gnd value is 1vcc value is 3sig value is 0tmp value is 0

birth month is 7birth month is 8birth month is after aprilbirth month is 5birth month is 40birth month is may

Page 13: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

13 © tjEE 1910

Type Definition

• C Types

Derived

Function Array Pointer Structure Union Enumerated

Page 14: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

14 © tjEE 1910

Structures

• Structure

• Collection of related elements

• Not necessarily the same type

• Sharing a single name

Page 15: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

15 © tjEE 1910

Structures

• Structure

• Elemental unit is called a Field

• Looks just like a variable• has a type

• takes up memory space

• can be assigned values

• can be read

• Only difference is that a Field is part of a Structure

Page 16: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

16 © tjEE 1910

Structures

• Structure

fraction

denominatornumerator

ID name GPA

student

Page 17: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

17 © tjEE 1910

Structures

• Structure

• TAG definition

struct TAG{

field list

};

struct STUDENT{

char ID[10];

char name[26];

float GPA;

};

Page 18: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

18 © tjEE 1910

Structures

• Structure

• TypeDef definition• defines a new type• new elements of the structure type can be created via declaration

typedef struct{field list

} TYPE;

typedef struct{char ID[10];char name[26];float GPA;

} STUDENT; // type definition name

Page 19: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

19 © tjEE 1910

Structures

• Structure

• TypeDef definition

typedef struct{

char ID[10];

char name[26];

float GPA;

} STUDENT; // type definition name

int foo;

STUDENT student1;

STUDENT student2;

Page 20: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

20 © tjEE 1910

Structures

• Structure

• TypeDef definition• Initialization

int foo;

STUDENT student1 = {12A56B, Joe Smith, 3.45};

STUDENT student2 = {12A56B, Joe Smith};

GPA defaults to 0.0

If not specified, int and float fields default to 0, 0.0

char fields default to null - \0

Page 21: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

21 © tjEE 1910

Structures

• Structure

• Access

structure.field

student1.ID

student1.name

student1.GPA

Page 22: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

22 © tjEE 1910

Structures

• Structure

• Access

student2.GPA = 2.5;

if(student1.GPA >= 3.5){

}

printf(“student GPA: %.2f”, student2.GPA);

scanf(“%f”, &student1.GPA);

Page 23: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

23 © tjEE 1910

Structures

• Structure

• Manipulation• Only one operation – assignment

student2 = student1;

Page 24: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

24 © tjEE 1910

Structures

• Structure• Pointerstypedef struct{

char ID[10];

char name[26];

float GPA;

} STUDENT;

STUDENT* student_ptr; // define a pointer of STUDENT type

student_ptr = &student1; // student_ptr now points to student1

Page 25: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

25 © tjEE 1910

Structures

• Structure• Pointers

STUDENT* student_ptr; // define a pointer of STUDENT type

student_ptr = &student1; // student_ptr now points to student1

(*student_ptr).GPA = 3.66; // dereference

student_ptr->GPA = 3.66; // indirect selection

Page 26: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

26 © tjEE 1910

Structures#include <stdio.h>#include <unistd.h>

// Type definitionstypedef struct{ // define a type: CLOCK

int hr;int min;int sec;

} CLOCK;

// Function prototypesvoid increment (CLOCK* clock);void display (CLOCK clock);

int main(void){setbuf(stdout, NULL); // disable buffering

// Local variablesCLOCK clock = {11, 59, 57};

// Operationfor(; ; ){

increment(&clock);display(clock);sleep(1);

}return 0;

}

void increment(CLOCK* clock){(clock->sec)++; // increment secondsif (clock->sec == 60){

clock->sec = 0;(clock->min)++; // increment minutesif(clock->min == 60){

clock->min = 0;(clock->hr)++;if(clock->hr == 12){

clock->hr = 0;} // end if hr

} // end if min} // end if secreturn;

}

void display(CLOCK clock){printf("%02d:%02d:%02d\n", clock.hr, clock.min, clock.sec);return;

}

Pointer to structurepointer notation for fields

structure passedstructure notation for fields

11:59:5611:59:5711:59:5811:59:5900:00:0000:00:0100:00:02

Page 27: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

27 © tjEE 1910

Structures

#define P3 ((DIO_PORT_Odd_Interruptable_Type*) (DIO_BASE + 0x0020))

P3->OUT … location of all I/Os

location of P3structure type name

defines a pointer to a structure located in a specific spot

Page 28: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

28 © tjEE 1910

Structures

P3->OUT …

__IO uint8_t OUT; /*!< Port Output */

accesses an element in a structure pointer

typedef struct {__I uint8_t IN; /*!< Port Input */uint8_t RESERVED0;__IO uint8_t OUT; /*!< Port Output */uint8_t RESERVED1;__IO uint8_t DIR; /*!< Port Direction */uint8_t RESERVED2;__IO uint8_t REN; /*!< Port Resistor Enable */uint8_t RESERVED3;__IO uint8_t DS; /*!< Port Drive Strength */uint8_t RESERVED4;__IO uint8_t SEL0; /*!< Port Select 0 */uint8_t RESERVED5;__IO uint8_t SEL1; /*!< Port Select 1 */uint8_t RESERVED6;__I uint16_t IV; /*!< Port Interrupt Vector Value */uint8_t RESERVED7[6];__IO uint8_t SELC; /*!< Port Complement Select */uint8_t RESERVED8;__IO uint8_t IES; /*!< Port Interrupt Edge Select */uint8_t RESERVED9;__IO uint8_t IE; /*!< Port Interrupt Enable */uint8_t RESERVED10;__IO uint8_t IFG; /*!< Port Interrupt Flag */uint8_t RESERVED11;

} DIO_PORT_Odd_Interruptable_Type;

Page 29: Enum and Struct - Milwaukee School of Engineering...EE 1910 4 © tj Enumerated Types •Enum •Declare a new Type •Define its name and its members (enumerate them) •Members are

29 © tjEE 1910

Structures

typedef struct {__I uint8_t IN; /*!< Port Input */uint8_t RESERVED0;__IO uint8_t OUT; /*!< Port Output */uint8_t RESERVED1;__IO uint8_t DIR; /*!< Port Direction */uint8_t RESERVED2;__IO uint8_t REN; /*!< Port Resistor Enable */uint8_t RESERVED3;__IO uint8_t DS; /*!< Port Drive Strength */uint8_t RESERVED4;__IO uint8_t SEL0; /*!< Port Select 0 */uint8_t RESERVED5;__IO uint8_t SEL1; /*!< Port Select 1 */uint8_t RESERVED6;__I uint16_t IV; /*!< Port Interrupt Vector Value */uint8_t RESERVED7[6];__IO uint8_t SELC; /*!< Port Complement Select */uint8_t RESERVED8;__IO uint8_t IES; /*!< Port Interrupt Edge Select */uint8_t RESERVED9;__IO uint8_t IE; /*!< Port Interrupt Enable */uint8_t RESERVED10;__IO uint8_t IFG; /*!< Port Interrupt Flag */uint8_t RESERVED11;

} DIO_PORT_Odd_Interruptable_Type;

#define P3 ((DIO_PORT_Odd_Interruptable_Type*) (DIO_BASE + 0x0020))

P3->OUT …

location of all I/Os

location of P3

__IO uint8_t OUT; /*!< Port Output */