Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.

Similar presentations


Presentation on theme: "Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we."— Presentation transcript:

1 Structures

2 An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we wanted to store personnel information like name, ID, address and SSN, all of different types then using arrays will be impractical Structures allow us to combine variables of different types with a single name A convenient way of grouping several pieces of related information together

3 Structures In addition to the primitive data types such as int, char, float, structures can have members that are other structures, arrays or pointers Can get complicated however.

4 Defining Structures A structure type is usually defined using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.

5 Defining Structures General syntax for defining structures is: typedef struct { ;.... ; } ;

6 Defining Structures The following is a definition of a new type typedef struct { char name[64]; char course[128]; int year; } student_info; variables of type student_info can be declared as follows. student_info student_1; // same as declaring int year; // all structures declared as of type student_info will have members // name, course and year

7 Accessing members of structures All members of a structure behave exactly the way they would if they were being used independently However, to access members of a structure we have to use the. (dot) operator Consider the above declaration: student_info student_1; To assign values to its members, the syntax is: strcpy(student_1.name, “John”); strcpy(student_1.course, “COP 3223H”); student_1.year = 2014;

8 Example - Accessing members of structures Write a program to declare a new data type called student_info using the struct and typedef keywords. The struct student_info should have student’s name, course and graduation year as members. Ask the user to input values and store them in appropriate members of a student_info struct

9 Structures – Representing date Write a program to declare a new data type called date to store year, month and day using the struct and typedef keywords.

10 Example – Structures within structures Write a new version of the program to declare a new data type called date to store a student’s name, course and graduation date. The date member in this program is the date structure created in the previous example

11 Other Example of structures Complex numbers have two parts, a real value and an imaginary part. Structures are best suited for these Act as databases e.g. an array of structures would hold all relevant information of all students in a course Other data structures like trees, graphs, heaps, stacks and queues make use of structures Towards the end of our course, we will study linked lists that make use of structures

12 Arrays of structures Just as arrays for primitive data types, we can have arrays of structures E.g., an array of student_info structures would be declared as: student_info student[3]; Elements of the struct array are accessed by specifying their index locations and the. (dot) operator: scanf(“%s”, &student[0].name);

13 Arrays of structures A visual representation of an array of structures name course grad_year Student[0] name course grad_year Student[1] name course grad_year Student[2]


Download ppt "Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we."

Similar presentations


Ads by Google