ch7 structures
Post on 07-Nov-2014
1.423 Views
Preview:
DESCRIPTION
TRANSCRIPT
- 1. CHAPTER 7: STRUCTURES
- 2. Outline:
- Objectives
- 7.1 Introduction
- 7.2 Defining a Structure
- 7.3 Defining a Structure using typedef
- 7.4 Structure Members
- 7.5 Initializing Structures
- 7.6 Nested Structures
- 7.7 Array of Structures
- 7.8 Structures and Pointers
- 7.9 Passing Structures to Functions
- 3. Objectives
- Defining structures in various ways of structure declaration.
- Initializing members of a structure variable.
- Defining element array of structures.
- Accessing individual structure members.
- Defining structures using typedef .
- Accessing members of a structure using pointer.
- Passing structures as arguments to functions.
- 4. 7.1 Introduction
- A structure can have individual elements different in type .
- contain integer elements, floating-point elements and character elements.
- included elements: pointers, arrays and other structures.
- The individual structure elements are referred to as members .
- 5. 7.2 Defining a Structure
- General terms:
- where:
-
- struct is a reserved word.
-
- tag is a name that identifies structures of this type.
-
- member1, member2, ..., memberN are individual member declarations.
-
- must end with semicolon ( ; ).
- 6. 7.2 Defining a Structure cont1
- The individual members :
-
- can be ordinary variables, pointers, arrays or other structures.
-
- cannot be initialized within a structure-type declaration.
- The member names:
-
- within a particular structure must be distinct from one another .
-
- can be the same as the variable name that is defined outside of the structure.
- A storage class cannot be assigned to an individual member.
- 7. 7.2 Defining a Structure cont2
- Example 7.1: Typical structure declarations.
- Explanations:
-
- Structure named custAccount .
-
- The custAccount contains 4 members and 2 structure variables .
- 8. 7.2 Defining a Structure cont3
- Example 7.2: This declaration is equivalent with Example 7.1.
- 9. 7.2 Defining a Structure cont4
- Example 7.3: A structure variable may be defined as a member of another structure.
- The declaration of the embedded structure must appear before the declaration of the outer structure.
- Eg.: Structure date is declared before structure custAccount .
- 10. 7.2 Defining a Structure cont5
- 11. 7.3 Defining using typedef
- Alternative approach on defining a structure.
- Made once at the head of the program.
- Subsequent declarations: using the new name .
- 12. 7.3 Defining using typedef - cont1
- Features:
-
- Allows users to define new data-types that are equivalent to existing data-types.
-
- Eliminates the need to repeated write struct tag whenever a structure is referenced.
- 13. 7.3 Defining using typedef - cont2
- General user-defined structure type:
- The new_type refers to the user-defined structure type.
- Structure variables can be defined in terms of the new data type.
- 14. 7.3 Defining using typedef - cont3
- Example 7.14: Alternative declaration using typedef .
- The record is defined as a user-defined data type.
- The second declaration defines oldcust and newcust as structure variables of type record .
- 15. 7.3 Defining using typedef - cont3
- The typedef features can be used repeatedly , to define one data type in terms of other user-defined data types.
- Example 7.5 shows:
-
- the date and record are user-defined structure types.
-
- The cust is a 100-element array of type record .
- 16. 7.3 Defining using typedef - cont4
- Exp. 7.6 and Exp. 7.7 shows the variation of Exp. 7.5.
- 17. 7.4 Structure Members
- The members of a structure are usually processed individually to access the individual structure members.
- It can be accessed by:
- 18. 7.4 Structure Members - cont1
- Example 7.8
- 19. 7.5 Initializing Structures
- The members of a structure variable can be assigned initial values.
- The initial values must appear in the order in which they will be assigned to their corresponding structure members.
- 20. 7.5 Initializing Structures cont1
- The general form:
- 21. 7.5 Initializing Structures cont1 struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; }; static struct custAccount cust= {2345,S,Khadijah Ismail,2400.90,9,7,2006}; : Example 7.9
- 22. 7.5 Initializing Structures cont2 Example 7.10:
- 23. 7.6 Nested Stuctures
- saves time when writing programs that use similar structure.
- The common members have to define only once in their own structure and then use that structure as a member in another structure.
- 24. 7.6 Nested Structures cont1 struct employees{ char empName[25]; char address[30] ; char city [10] ; char state[2] ; long int poscode ; double salary; }; struct customers{ char custName[25]; char address[30] ; char city [10] ; char state[2] ; long int poscode ; double balance; }; struct addressInfo{ char address[30]; char city [10]; char state[2]; long int poscode; }; struct employees{ char empName[25]; struct addressInfo eAddress; double salary; }e1; struct customers{ char custName[25]; struct addressInfo cAddress; double balance; }c1; Example 7.12: :Example 7.13
- 25. 7.7 Arrays of Structures
- Example 7.14: Declaration of a 200-element array of structures.
- 26. 7.7 Arrays of Structures cont2 Example 7.16
- 27. 7.7 Arrays of Structures cont1
- Example 7.15: Assigning initial values to an array of structures
- 28. 7.8 Structures and Pointers
- A pointer to a structure type variable is declared by a statement:
- A structure member could be accessed by pointers with the following statements:
- 29. 7.8 Structures and Pointers cont1 Example 7.16: Example 7.17:
- 30. 7.9 Passing Structures to Func
- There are several different ways to pass structure-type information to or from a function.
-
- transferred individually , or
-
- entire structures can be transferred.
- 31. 7.9 Passing Structures cont1
- Individual structure members can be:
-
- passed to a function as arguments in the function call, and
-
- a single structure member can be returned by the return statement.
- Each structure member is treated the same as an ordinary single-valued variable.
- 32. 7.9 Passing Structures cont2
- Example 7.18:
-
- The program transfer partial structure member to a function, addition.
- 33. 7.9 Passing Structures cont3
- Example 7.19:
-
- The program transfer a structure-type pointer to a function, adjust .
top related