Enum and Struct
Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 2 tj
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 3 tj
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}; 4 tj
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) 5 tj
Enumerated Types Enum Assign values power = BLACK; gnd = WHITE; signal = RED; enum wirecolor input; input = signal; // input now RED 6 tj
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){ 7 tj
Enumerated Types Enum 0 1 2 10 11 enum month {JAN, FEB, MAR, enum month birthmonth; enum month currentmonth; if (birthmonth > currentmonth){ NOV, DEC}; switch(currentmonth){ case JAN: // case 0 case FEB: // case 1 8 tj
Enumerated Types Enum 0 1 2 10 11 enum month {JAN, FEB, MAR, NOV, DEC}; suppose we d like the member numbers to match some other pattern 1 2 3 11 12 enum month {JAN=1, FEB, MAR, NOV, DEC}; 9 tj
Enumerated Types Enum Anonymous Enumeration enum {OFF, ON}; // assign OFF the value 0, ON: 1 enum {SPACE =, COMMA =,, COLON = : }; 10 tj
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 them enum 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 variables gnd = 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"); else printf("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 11 tj
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 them enum 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 variables gnd = 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); gnd value is 1 vcc value is 3 sig value is 0 tmp value is 0 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"); else printf("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 birth month is 7 birth month is 8 birth month is after april birth month is 5 birth month is 40 birth month is may 12 tj
Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 13 tj
Structure Collection of related elements Not necessarily the same type Sharing a single name 14 tj
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 15 tj
Structure numerator denominator fraction ID name GPA student 16 tj
Structure TAG definition struct TAG{ field list }; struct STUDENT{ char ID[10]; char name[26]; float GPA; }; 17 tj
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 18 tj
Structure TypeDef definition typedef struct{ char ID[10]; char name[26]; float GPA; } STUDENT; // type definition name int foo; STUDENT student1; STUDENT student2; 19 tj
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 20 tj
Structure Access structure.field student1.id student1.name student1.gpa 21 tj
Structure Access student2.gpa = 2.5; if(student1.gpa >= 3.5){ } printf( student GPA: %.2f, student2.gpa); scanf( %f, &student1.gpa); 22 tj
Structure Manipulation Only one operation assignment student2 = student1; 23 tj
Structure Pointers typedef struct{ char ID[10]; char name[26]; float GPA; } STUDENT; STUDENT* student_ptr; student_ptr = &student1; // define a pointer of STUDENT type // student_ptr now points to student1 24 tj
Structure Pointers STUDENT* student_ptr; student_ptr = &student1; // define a pointer of STUDENT type // student_ptr now points to student1 (*student_ptr).gpa = 3.66; student_ptr->gpa = 3.66; // dereference // indirect selection 25 tj
#include <stdio.h> #include <unistd.h> // Type definitions typedef struct{ // define a type: CLOCK int hr; int min; int sec; } CLOCK; // Function prototypes void increment (CLOCK* clock); void display (CLOCK clock); int main(void){ setbuf(stdout, NULL); // disable buffering // Local variables CLOCK clock = {11, 59, 57}; // Operation for(; ; ){ increment(&clock); display(clock); sleep(1); } return 0; } void increment(clock* clock){ (clock->sec)++; // increment seconds if (clock->sec == 60){ clock->sec = 0; (clock->min)++; // increment minutes if(clock->min == 60){ clock->min = 0; (clock->hr)++; if(clock->hr == 12){ clock->hr = 0; } // end if hr } // end if min } } // end if sec return; Pointer to structure pointer notation for fields structure passed structure notation for fields void display(clock clock){ printf("%02d:%02d:%02d\n", clock.hr, clock.min, clock.sec); return; } 11:59:56 11:59:57 11:59:58 11:59:59 00:00:00 00:00:01 26 00:00:02 tj
P3->OUT location of all I/Os #define P3 ((DIO_PORT_Odd_Interruptable_Type*) (DIO_BASE + 0x0020)) structure type name location of P3 defines a pointer to a structure located in a specific spot 27 tj
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; 28 tj
location of all I/Os #define P3 ((DIO_PORT_Odd_Interruptable_Type*) (DIO_BASE + 0x0020)) P3->OUT location of P3 IO uint8_t OUT; /*!< Port Output */ 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; 29 tj