Ch7 structures

33
CHAPTER 7: STRUCTURES

description

 

Transcript of Ch7 structures

  • 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 ( ; ).
    struct tag { member1 ; member2 ; ... memberN ; };
  • 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 .
    struct custAccount { int accNo; char accType; char name[80]; float accBalance; }; struct custAccount oldcust, newcust;
  • 8. 7.2 Defining a Structure cont3
    • Example 7.2: This declaration is equivalent with Example 7.1.
    struct custAccount { int accNo; char accType; char name[80]; float accBalance; } oldcust, newcust;
  • 9. 7.2 Defining a Structure cont4
    • Example 7.3: A structure variable may be defined as a member of another structure.
    struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; } oldcust, newcust;
    • 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:
    typedef struct { member1; member2; ...; memberN; } new_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 .
    typedef struct { int accNo; char accType; char name[80]; float accBalance; } record; record oldcust, newcust;
    • 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 .
    typedef struct { int month; int day; int year; } date; typedef struct { int accNo; char accType; char name[80]; float accBalance; date payment; } record; record cust[100];
  • 16. 7.3 Defining using typedef - cont4
    • Exp. 7.6 and Exp. 7.7 shows the variation of Exp. 7.5.
    typedef struct { int month; int day; int year; } date; typedef struct { int accNo; char accType; char name[80]; float accBalance; date payment; } record[100]; record cust; typedef struct { int month; int day; int year; } date; struct { int accNo; char accType; char name[80]; float accBalance; date payment; } cust[100];
  • 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:
    variable.member variables name act as a separator members name
  • 18. 7.4 Structure Members - cont1
    • Example 7.8
    struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; } cust; Example: To access the info. of cust. /* to access the customers */ /* account no. */ cust.accNo /* to access the customers */ /* name */ cust.name cust.name[2] &cust.name[2] /* to access the month of */ /* the payment of customer */ cust.payment.month
  • 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:
    storage_class struct tag variable = {value1, value2, ..., valueN}; Refer to the value of corresponding structure members auto, extern, register, static
  • 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.
    struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; } cust[200]; struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; }; struct custAccount cust[200]; OR
  • 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
    struct student { char name[80]; float courseMark; float finalMark; int TotMark; }; static struct student MarkList[ ] = { Kadir, 45.50, 30.00, 76, "Azizan", 43.50, 30.00, 73, "Xavier", 44.50, 30.00, 75, "Nantha", 46.50, 30.00, 77, "Junani", 42.50, 30.00, 73, "Martha", 42.00, 30.00, 72 } ;
  • 28. 7.8 Structures and Pointers
    • A pointer to a structure type variable is declared by a statement:
    struct name *ptr ;
    • A structure member could be accessed by pointers with the following statements:
    struct PersonalData *ptr ; (*ptr).YearOfBirth=20 ; struct PersonalData *ptr ; ptr -> YearOfBirth=20 ; OR
  • 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 .