1 / 16

Structures

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.

astro
Download Presentation

Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures EE2372 Software Design I Dr. Gerardo Rosiles

  2. 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.

  3. 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

  4. Introduction • structure driver license { • Name • DOB • DL # • Height • Eyes • Sex • Address • Expiration Date • Picture }

  5. 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 }

  6. 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;

  7. 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]; };

  8. 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.

  9. 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; };

  10. 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);

  11. 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);

  12. 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;

  13. 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.

  14. 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

  15. 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

  16. 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.

More Related