Lecture 13
Transkript
Lecture 13
Izmir Institute of Technology CE - 104 Lecture 13 References: - C: A software Engineering Approach 1 In this course you will learn ● ● ● ● ● Structure Data Types Use “.” operator to access members of a structure See how to declare pointers to structure variables. Use “ -> ” operator to access members of a structure when using pointers. Union Data Types 2 Structures and Unions Arrays are good for dealing with groups of identical typed variables, but they are unsatisfactory for managing groups of differently typed data. To service groups of mixed data you need to use an aggregate type called a structure. Another aggregate type, called a union, enables you to interpret the same memory locations in different ways. 3 Structures To keep track of your grades in the CE 104 course we need to associate student names, their student id numbers, and their grades corresponding to homeworks and examinations. And of course, finally their letter grade. Without structures we would need to define the followings: The name shall consist of 18 characters, the student id number is a 9 digit number and therefore should be assigned as a long int, and the grades of homeworks and exams should be a short int (char would also work). And finally the grade shall consist of two characters. char name[19], grade[3]; long int idnum; short hw, exam; 4 Structures And if there are 29 students in this class: char name[29][19], grade[29][3]; long int idnum[29]; short hw[29], exam[29]; Then for each student we would write strcpy( name[0], "Temel Karadeniz" ); the following idnum[0]=30909090; hw[0]=100; exam[0]=100; strcpy( grade[0], "AA" ); Storing the data in this form gets the information into the computer, but it creates a strange organization. The information about one person is scattered about memory instead of being grouped together. 5 Structures A more natural organization would be to create a single variable that contains all three pieces of data. C enables you to do this with a data type called a structure. A structure is like an array except that each element can have a different data type. Moreover, the elements in a structure, called fields or members, have names instead of subscript values. We like to think of structures as arrays with personality. To declare a structure to hold CE 104 statistics, you would write struct ce104stat typedef struct { { char ce104_name[19], ce104_grade[3]; char ce104_name[19], ce104_grade[3]; long int ce104_idnum; long int ce104_idnum; short ce104_hw, ce104_exam; short ce104_hw, ce104_exam; } ce104stat ; }; ce104stat ce104; ce104stat ce104; 6 Structures #include <stdio.h> main() { typedef struct { char name[19]; long int idnum; short hw, exam; char grade[3]; } COURSESTAT; COURSESTAT ce104[29], *ptr ; ptr=&ce104[0]; int i=0; strcpy(ptr->name,"Temel Karadeniz"); ptr->idnum = 1080033211; ptr->hw = 85; ce104[i].exam = 98; strcpy(ce104[i].grade,"AA"); } printf(" name: %s\n", ce104[i].name); printf("idnum: %ld\n", ptr->idnum); printf(" hw: %d\n", ptr->hw); printf(" exam: %d\n", ce104[i].exam); printf("grade: %s\n", ce104[i].grade); 7 Structures Data Type A structure is a user created type definition. It can hold a number of sub-variables under a single variable. typedef struct { char *isim; long tc_kimlik; short dt_yil, dt_ay, dt_gun; } kisisel_bilgi; 8 Structures Data Type Create a variable that stores the Name, T.C. Identity number, and the date of birth of each student. #include <stdio.h> main() { typedef struct { char *isim; long tc_kimlik; short dt_yil, dt_ay, dt_gun; } KISISEL_BILGI ; KISISEL_BILGI KISISEL_BILGI KISISEL_BILGI KISISEL_BILGI kisi_1; kisi_2; kisi_3; kisi_4; // To be continued // Continued ... kisi_1.isim = "Temel Karadeniz"; kisi_1.tc_kimlik = 1231231231000; kisi_1.dt_yil = 1995; kisi_1.dt_ay = 2; kisi_1.dt_gun = 29; kisi_2.isim = "Dursun Akdeniz"; kisi_2.tc_kimlik = 1231231231010; kisi_2.dt_yil = 1994; kisi_2.dt_ay = 11; kisi_2.dt_gun = 30; } Structures Data Type Print personal information: printf("İsim Soyisim : %s\n", kisi_1.isim); printf("T.C. Kimlik No: %ld\n", kisi_1.tc_kimlik); printf("Dogum Tarihi : %2d.%2d.%4d\n", kisi_1.dt_gun, kisi_1.dt_ay, kisi_1.dt_yil); printf("\n"); printf("İsim Soyisim : %s\n", kisi_2.isim); printf("T.C. Kimlik No: %ld\n", kisi_2.tc_kimlik); printf("Dogum Tarihi : %2d.%2d.%4d\n", kisi_2.dt_gun, kisi_2.dt_ay, kisi_2.dt_yil); Structures Data Type #include <stdio.h> main() { typedef struct { char *isim; long tc_kimlik; short dt_yil, dt_ay, dt_gun; } KISISEL_BILGI ; KISISEL_BILGI kisiler[10]; kisiler[0].isim = "Temel Karadeniz"; kisiler[0].tc_kimlik = 1231231231000; kisiler[0].dt_yil = 1995; kisiler[0].dt_ay = 2; kisiler[0].dt_gun = 29; Structures Data Type kisiler[1].isim = "Dursun Akdeniz"; kisiler[1].tc_kimlik = 1231231231010; kisiler[1].dt_yil = 1994; kisiler[1].dt_ay = 11; kisiler[1].dt_gun = 30; int i; for (i=0;i<10;i++) { printf(“\n”); printf("İsim Soyisim : %s\n", kisiler[i].isim); printf("T.C. Kimlik No: %ld\n", kisiler[i].tc_kimlik); printf("Dogum Tarihi : %2d.%2d.%4d\n", kisiler[i].dt_gun, kisiler[i].dt_ay, kisiler[i].dt_yil); } } Structures Data Type Example: Vector Arithmetics #include <stdio.h> #include <stdlib.h> typedef struct { double x; /* user defined data-type called vector */ /* this is a definition*/ /* no variables are declared */ double y; /* location is outside any block */ } vector; vector add(vector vec1, vector vec2) ; /* prototype */ vector sub(vector vec1, vector vec2) ; /* prototype */ double dot(vector vec1, vector vec2) ; /* prototype */ /* continued on next slide */ 13 Structures Data Type /* see explanation of the above in subsequent slides */ void main(void){ vector v1,v2,v3 ={1.0,2.0}; /* three variables of type vector*/ /* v3 initialized to {1.0,2.0} */ v1.x = 2.5; /* use the . to access individual fields */ v1.y = -1.5; v2 = v3; */ /* can assign all the values with one statement /* continued on next slide */ Structures Data Type printf(“v1 = [%f,%f]\n”, v1.x, v1.y); printf(“v2 = [%f,%f]\n”, v2.x, v2.y); v3 = add(v1,v2); printf(“add(v1,v2) = [%f,%f]\n”, v3.x, v3.y); v3 = sub(v1,v2); printf(“sub(v1,v2) = [%f,%f]\n”, printf(“dot(v1,v2) = %f\n”, return 0; } /* end of main */ v3.x, v3.y); dot(v1,v2)); Structures Data Type vector add(vector vec1, vector vec2){ vector v4 = {vec1.x + vec2.x, vec1.y + vec2.y}; return v4; } /* end of add */ vector sub(vector vec1, vector vec2){ vector v4 = {vec1.x - vec2.x, vec1.y - vec2.y}; return v4; } /* end of sub */ double dot(vector vec1, vector vec2){ return (vec1.x * vec2.x) + (vec1.y * vec2.y); } /* end of dot */ More Examples ? #include <stdio.h> #include <stdlib.h> main() { typedef struct { int appletrees; /* number of apple trees */ int hazelnuttrees; /* number of hazelnut trees */ } mygarden; mygarden.appletrees = 3; mygarden.hazelnuttrees = 4; } Union Data Types Unions are similar to structures except that the members are overlaid one on top of another, so members share the same memory. #include <stdio.h> main() { typedef union { short int number_of_students; short int ogrenci_sayisi; } DERS_INFO; DERS_INFO ce104; ce104.ogrenci_sayisi = 5; printf("\n No. of students = %d \n", ce104.number_of_students); } In this course you have learned ● ● ● ● ● Structure Data Types Use “.” operator to access members of a structure See how to declare pointers to structure variables. Use “ -> ” operator to access members of a structure when using pointers. Union Data Types 19