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 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 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 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 ” operator to access members of a structure when using pointers. Union Data Types

19