Structures
E N D
Presentation Transcript
Structures EE2372 Software Design I Dr. Gerardo Rosiles
Introduction • Arrays let you group elements of the same type. • In many cases we want to group elements of different types that are seen as belonging to the same entity. • Consider the information in your drivers license. • It is stored as a “data record” on a computer which can be accessed by an officer when you are stopped for a ticket or something.
Introduction • This data record is also known as a data structure in programming parlance. • What are the elements of driver’s license structures: http://www.usgwarchives.net/tx/bexar/photos/gph6texasdri.jpg
Introduction • structure driver license { • Name • DOB • DL # • Height • Eyes • Sex • Address • Expiration Date • Picture }
Introduction • A structure allows us to encapsulate this information of a well organized way. • Each element of the structure can be further split: structure name { first MI last } structure date { month day year } structure address { number street City State ZIP }
C structures • C allows us to create structures that combine different types. • You use the struct key word with the following syntax: structstructure_name { type1 identifier1; type2 identifier2; type3 identifier3; etc. } • The we can create variables that hold all the information as follows: struct_name var1, var2;
C structures – Examples struct date { int month; int day; int year; }; structstudent_data { char name[SIZE]; int age; float grade; }; Note that we have included an array in this structure. structfull_name { char first[20]; char MI; char last [30]; };
Quick introduction to Strings • Strings are a way to group characters into words and sentences using arrays of characters. • A quick way to create a string is as follows: char word[20]; word = “Hello”; • As long as you use less than 19 characters you are fine. WHY?? We will discuss this in Chapter 10.
Using C structures structstudent_data student1, student2; student1.name = “sam”; student1.age = 20; student1.grade = 3.5; printf("name: %s\n",student1.name); printf("age: %d\n",student1.age); printf("grade: %f\n",student1.grade); student2=student1; printf("name: %s\n",student2.name); printf("age: %d\n",student2.age); printf("grade: %f\n",student2.grade); } #include<stdio.h> #include<conio.h> #define SIZE 10 main() { structstudent_data { char name[SIZE]; int age; float grade; };
Using C structures • To access the elements of a structure we use the “dot” operator. • Initialization • Accessing for printing • Copying structure content struct data student1, student2; student1.name = “sam”; student1.age = 20; student1.grade = 3.5; printf("name: %s\n",student1.name); printf("age: %d\n",student1.age); printf("grade: %f\n",student1.grade); student2=student1; printf("name: %s\n",student2.name); printf("age: %d\n",student2.age); printf("grade: %f\n",student2.grade);
Using C structures • There are other ways to initialize structures • Initialize at declaration time struct data student1={"sam",20,3.5}; • Use scanf for user defined values printf(“Enter student name: “); scanf(“ %s \n”, &student1.name); printf(“Enter student age: “); scanf(“%d \n”, &student1.age); printf(“Enter student grade: “); scanf(“%f \n”, &student1.grade);
Using C structures • Individual components of structures can be used in any operation as any variable: arithmetic, logical, relational. if (student1.grade < 1.0) printf(“College of Business \n”); if( student_birthday_passed(student.age) ) student.age = student.age + 1;
Using C structures • More interesting use of structures: • Structures containing arrays. • We have seen this for the name strings. • Structures containing structures. • Arrays of structures.
Structure containing structures structfull_name { char first_name[SIZE]; char last_name[SIZE]; }; struct address { char street[SIZE]; unsigned intzipcode; }; struct position { char title[SIZE]; float salary; }; struct worker { structfull_name name; struct address home_address; char tel[SIZE]; struct position category; }; • Perhaps the most useful feature of structures. • Allows structured implementation of data records. • Example 2
Arrays of structures • Same definition as with arrays structstudent_data EE2372_students[30]; • We can access each structure through an index and then each element of the structure through the dot operator. EE2372_students[17].name = “Gerardo”; EE2372_students[17].age = 25; EE2372_students[17].grade = 4.0; • Example 3
Functions and structures • We can pass structures to functions as parameters • Functions can return structures • Example 5 • Are parameters passed by value of by reference.? • Let’s modify Example 4.