Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.

Similar presentations


Presentation on theme: "Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual."— Presentation transcript:

1 Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual items that comprise a structure can be any of the basic data types such as char, int, float,and double. They can also include arrays or even other structures. Structures enable us to build our own data types and to group data of different types under a common name. To create a structure you must: 1. Define the structure. 2. Declare variables of that type.

2 11.1 The Structure Template As an example of the syntax for defining a structure, consider the following structure definition. struct rectangle { float x1; float y2; int color; int id_number; int level; };

3 11.1.2 Declaring Structures To create structure variable, we have to declare variable of the type defined in the structure definition. For example, to create variable of the type rectangle, we use the following declaration struct rectangle old_rect, curr_rect, new_rect; This declaration creates three variable, old_rect, curr_rect, and new_rect, of type rectangle. It also allocates storage for each of these variables.

4 It is possible to define a structure and declare variables of the specified structure type in a single statement as in the following. struct rectangle { float x1; float y2; int color; int id_number; int level; } old_rect, curr_rect, new_rect;

5 11.1.3 Accessing Structure Members A special syntax in needed for accessing the members of a structure. A member of a structure is accessed by specifying the name of the structure variable, followed by the structure member of operator (.) and the member name. structure_variable. member_name Thus the members of the structure variable new_rect of the structure rectangle can be accessed using the following expressions. new_rect.x1; new_rect.y1; new_rect.color; new_rect.id_number; new_rect.level;

6 Example: Accessing Structure Members #include struct student_record { char last_name[41]; char first_name[21]; } s41g0001; void main(void) { strcpy(s41g0001.first_name, “John”); strcpy(s41g0001.last_name, “Antony”); printf(“\n First Name is : %s”, s41g0001.first_name); printf(“\n Last Name is : %s”, s41g0001, last_name); }

7 Program Output: First Name is : John Last Name is : Antony 11.2 The typedef Statement The typedef statement is used to create synonyms for data types. These data types can be the built-in C data types such as char, int, float, and double or any other user defined data types. Suppose we want to use the identifier REAL to represent variables of type float. We can do this using the following typedef statement typedef float REAL;

8 This statement defines the name REAL to be a synonym for float. We can now use the name REAL in our programs instead of float. For example, we can declare variables using the statement REAL a, b, c; This statement is equivalent to float a, b, c; The general syntax for the typedef statement is typedef

9 11.3 Unions C provides a variable type called a union that allows a variable to store values of different types with thr restriction that only one value can occupy the variable at any given time. Unions provide a way to economize on storage. The syntax for creating and using unions is the same as for structures. We first create a union template with an optional tag and then use this tag for creating union variables. The following code declares a union. Union input_value { float real_num; int num; char letter; } input1;

10 11.4 Enumerated Types Please read some information about C enumerated data type from the book.

11 Any Questions


Download ppt "Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual."

Similar presentations


Ads by Google