Computers and programming The 6th lecture Jiří Šebesta TOPIC 1. 2. 3. 4. 5. Structures Unions Enumerative type Dynamic variables - introduction Example Structures (1/4) • Structure: an array consisting of items of different types embedding into superior type • Declaration typedef struct friends { char fname[20]; char sname[20]; int age; char phone[20]; } T_friend; type identifier // my girlfriend // // // // her her her her first name surrname age phone number Structures (2/4) • Access to item of structure: int main(void) { T_friend baby; strcpy(baby.fname, "Anna\0"); strcpy(baby.sname, "Novakova\0"); baby.age = 16; strcpy(baby.phone, "+420776151443\0"); printf(" %s %s - %d let - tel.: %s", baby.fname, baby.sname, baby.age, baby.phone); getchar(); return 0; } Source code: Ex59.c variable item Structures (3/4) • Structure in structure: typedef struct date // date { int year; // year int month; // month int day; // day } T_date; typedef struct drivelog // drive log book { char driver[20]; // name char in_city[20];// initial city char en_city[20];// ending city T_date in_date; // initial date T_date en_date; // ending dta int dist; // distance int in_tacho; // initial st. of tacho. int en_tacho; // ending st. of tacho. } T_drivelog; Structures (4/4) • Manipulation with items of structure: T_drivelog toyota020, toyota021; strcpy(toyota020.driver, "John"); toyota020.in_date.year = 2011; toyota020.in_tacho = 53210; toyota020.en_tacho = 53372; toyota020.dist = toyota020.en_tacho-toyota020.in_tacho; strcpy(toyota021.driver, "Judith"); strcpy(toyota021.in_city, toyota020.en_city); toyota021.in_tacho = toyota020.en_tacho; toyota021.en_tacho = 53712; toyota021.dist = toyota021.en_tacho-toyota021.in_tacho; Source code: Ex60.c Unions (1/3) • Union: it enables to save values of different types to a single variable (allocated common space in memory) union u1 {int i; double d; char c;} U = {'u'}; name of union list of union items variable of declared type of unie • Memory size allocated for the union is given by the size of the largest element • STRUCTURE: int and double and char • UNION: int or double or char Unions (2/3) • Declaration typedef union id { int id_num; char id_str[10]; } T_id; // ID of item type identifier variable of declared type // numerical ID // textual ID int main(void) { char input_id[10]; int n, t; printf("\n\nInsert ID: "); gets(input_id); // get string from stdin … Unions (3/3) t=0; // test if at least char is not digit for(n=0; input_id[n]!='\0'; n++) if (input_id[n]<'0' || input_id[n]>'9') t=1; // t=0 input_id is a number, t=1 input_id is a string if (t==0) // input_id is a number { item.id_num=atol(input_id); printf("ID contents a number: %d\n", item.id_num); } else // input_id is a string { strcpy(item.id_str, input_id); printf("ID contents a string: %s\n", item.id_str); } S. code: Ex61.c Enumerative type (1/3) • Enumerative type: a set of symbolic constants with strictly defined values • Declaration: 0 1 2 enum cards {seven, eight, nine, 3 4 face_card, ace} set_1, set_2; type identifier variable of declared type 7 8 9 enum cards {seven=7, eight, nine, 20 30 face_card=20, ace=30} set_1, set_2; Enumerative type (2/3) • Operator for enumerative type: – only assignment enum cards {seven=7, eight, nine, face_card=20, ace=30} set_1, set_2; set_1 = nine; set_2 = 8; //not possible, 8 isn’t enumerator set_1 -=2; //not possible int num = seven //type cast of enumerator seven to int Enumerative type (3/3) enum zodiac {aries,taurus,gemini,cancer,leo,virgo, libra, scorpio, sagittarius, capricorn, …} eva; eva = virgo; switch(eva) { case aries: printf(”She is hard-headed”); break; case taurus: printf(”She is reserved”); break; case gemini: printf(”She is friendly”); break; case cancer: printf(”She is anxious”); break; case leo: printf(”She is bossy”); break; case virgo: printf(”She is trusty”); break; case libra: printf(”She is shaky”); break; … } Source code: Ex62.c Dynamic variables – intro (1/3) • Static variable: – memory space allocation when program is starting – memory space release when program is closing • Dynamic variable: – controlled memory space allocation when program is running by function malloc() in C – controlled memory space release when program is running by function free() in C Dynamic variables – intro (2/3) • Application of dynamic variables: – a memory space can be allocated and released if program is running any time – suited for large blocks of data (e.g. large arrays), which are temporal (e.g. results, which will not be used later) – if we work with unknown size of array, if we use a static variable, we have to allocate the space for the worst case (max. size), e.g. double x[10000] , this space is permanently blocked (in given function) Dynamic variables – intro (3/3) #include <stdio.h> #include <stdlib.h> allocation of space in a free variable memory with size of double void *malloc(size_t size) free address int main( void) { double *x, y; x = (double*)malloc(sizeof(double)); *x = 3.14; y = *x; free(x); printf("Ludolf's number: %f", y); printf("Ludolf's number: %f", *x); //free(x); getchar(); return 0; } Source code: Ex63.c type cast this space in a memory is pointed by x space release in memory pointed by x Example (1/5) • Create a program working as a car database. Each car is described by a type and a year of production. Data of cars are saved in dynamic variables, pointers are stored in an array. Adding a car is activated by pressing the key A, deleting by pressing D, and the program is stopped by pressing Q. In the database, at least one car has to be left. #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct car { char type[10]; int year; } T_car; T_car int *my_cars[20]; cnt=0; // car record // ptrs. to cars - global // recorded cars - global Example (2/5) Function which adds a car void addcar(char *atype, int ayear) { T_car *car; // pointer to a car car = (T_car*)malloc(sizeof(T_car)); strcpy(car->type, atype); // notation 1 (*car).year = ayear; // notation 2 my_cars[cnt++] = car; } Function which displays erased car void showdelcar(void) { printf("type: %s\n", my_cars[cnt]->type); printf("year: %d\n", my_cars[cnt]->year); } Example (3/5) Function which deletes a car void erasecar(void) { if(cnt>1) // if at { cnt--; // showdelcar(); // free(my_cars[cnt]); // } else printf("All cars can’t } least two cars one car to be removed print the removed car delete last be deleted"); Example (4/5) Main function - beginning int main(void) { char cmd, my_type[12]; int my_year; printf("\nA: Add, D: Delete, Q: Quit\n"); scanf("%c", &cmd); fflush( stdin); while(!(cmd == 'Q' || cmd == 'q')) { if(cmd=='A' || cmd=='a') { printf("\ntype : "); scanf("%s", &my_type); fflush(stdin); printf("\nyear : "); scanf("%d", &my_year); fflush(stdin); add(my_type, my_year); } Example (5/5) Main function - conclusion if( cmd=='D' || cmd=='d') erasecar(); printf("\nA: Add, D: Delete, Q: Quit"); scanf("%c", &cmd); fflush(stdin); } return 0; } Source code: Ex64.c TOPIC OF THE NEXT LECTURE Programming with files THANK YOU FOR YOUR ATTENTION