Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ch7 structures

1,900 views

Published on

Published in: Education, Technology, Business

Ch7 structures

  1. 1. CHAPTER 7: STRUCTURES
  2. 2. Outline: <ul><li>Objectives </li></ul><ul><li>7.1 Introduction </li></ul><ul><li>7.2 Defining a Structure </li></ul><ul><li>7.3 Defining a Structure using typedef </li></ul><ul><li>7.4 Structure Members </li></ul><ul><li>7.5 Initializing Structures </li></ul><ul><li>7.6 Nested Structures </li></ul><ul><li>7.7 Array of Structures </li></ul><ul><li>7.8 Structures and Pointers </li></ul><ul><li>7.9 Passing Structures to Functions </li></ul>
  3. 3. Objectives <ul><li>Defining structures in various ways of structure declaration. </li></ul><ul><li>Initializing members of a structure variable. </li></ul><ul><li>Defining element array of structures. </li></ul><ul><li>Accessing individual structure members. </li></ul><ul><li>Defining structures using typedef . </li></ul><ul><li>Accessing members of a structure using pointer. </li></ul><ul><li>Passing structures as arguments to functions. </li></ul>
  4. 4. 7.1 Introduction <ul><li>A structure can have individual elements different in type . </li></ul><ul><li>contain integer elements, floating-point elements and character elements. </li></ul><ul><li>included elements: pointers, arrays and other structures. </li></ul><ul><li>The individual structure elements are referred to as members . </li></ul>
  5. 5. 7.2 Defining a Structure <ul><li>General terms: </li></ul><ul><li>where: </li></ul><ul><ul><li>struct is a reserved word. </li></ul></ul><ul><ul><li>tag is a name that identifies structures of this type. </li></ul></ul><ul><ul><li>member1, member2, ..., memberN are individual member declarations. </li></ul></ul><ul><ul><li>must end with semicolon ( ; ). </li></ul></ul>struct tag { member1 ; member2 ; ... memberN ; };
  6. 6. 7.2 Defining a Structure – cont’1 <ul><li>The individual members : </li></ul><ul><ul><li>can be ordinary variables, pointers, arrays or other structures. </li></ul></ul><ul><ul><li>cannot be initialized within a structure-type declaration. </li></ul></ul><ul><li>The member names: </li></ul><ul><ul><li>within a particular structure must be distinct from one another . </li></ul></ul><ul><ul><li>can be the same as the variable name that is defined outside of the structure. </li></ul></ul><ul><li>A storage class cannot be assigned to an individual member. </li></ul>
  7. 7. 7.2 Defining a Structure – cont’2 <ul><li>Example 7.1: Typical structure declarations. </li></ul><ul><li>Explanations: </li></ul><ul><ul><li>Structure named custAccount . </li></ul></ul><ul><ul><li>The custAccount contains 4 members and 2 structure variables . </li></ul></ul>struct custAccount { int accNo; char accType; char name[80]; float accBalance; }; struct custAccount oldcust, newcust;
  8. 8. 7.2 Defining a Structure – cont’3 <ul><li>Example 7.2: This declaration is equivalent with Example 7.1. </li></ul>struct custAccount { int accNo; char accType; char name[80]; float accBalance; } oldcust, newcust;
  9. 9. 7.2 Defining a Structure – cont’4 <ul><li>Example 7.3: A structure variable may be defined as a member of another structure. </li></ul>struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; } oldcust, newcust; <ul><li>The declaration of the embedded structure must appear before the declaration of the outer structure. </li></ul><ul><li>Eg.: Structure date is declared before structure custAccount . </li></ul>
  10. 10. 7.2 Defining a Structure – cont’5
  11. 11. 7.3 Defining using typedef <ul><li>Alternative approach on defining a structure. </li></ul><ul><li>Made once at the head of the program. </li></ul><ul><li>Subsequent declarations: using the new name . </li></ul>
  12. 12. 7.3 Defining using typedef - cont’1 <ul><li>Features: </li></ul><ul><ul><li>Allows users to define new data-types that are equivalent to existing data-types. </li></ul></ul><ul><ul><li>Eliminates the need to repeated write struct tag whenever a structure is referenced. </li></ul></ul>
  13. 13. 7.3 Defining using typedef - cont’2 <ul><li>General user-defined structure type: </li></ul>typedef struct { member1; member2; ...; memberN; } new_type <ul><li>The new_type refers to the user-defined structure type. </li></ul><ul><li>Structure variables can be defined in terms of the new data type. </li></ul>
  14. 14. 7.3 Defining using typedef - cont’3 <ul><li>Example 7.14: Alternative declaration using typedef . </li></ul>typedef struct { int accNo; char accType; char name[80]; float accBalance; } record; record oldcust, newcust; <ul><li>The record is defined as a user-defined data type. </li></ul><ul><li>The second declaration defines oldcust and newcust as structure variables of type record . </li></ul>
  15. 15. 7.3 Defining using typedef - cont’3 <ul><li>The typedef features can be used repeatedly , to define one data type in terms of other user-defined data types. </li></ul><ul><li>Example 7.5 shows: </li></ul><ul><ul><li>the date and record are user-defined structure types. </li></ul></ul><ul><ul><li>The cust is a 100-element array of type record . </li></ul></ul>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. 16. 7.3 Defining using typedef - cont’4 <ul><li>Exp. 7.6 and Exp. 7.7 shows the variation of Exp. 7.5. </li></ul>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. 17. 7.4 Structure Members <ul><li>The members of a structure are usually processed individually to access the individual structure members. </li></ul><ul><li>It can be accessed by: </li></ul>variable.member variables’ name act as a separator members’ name
  18. 18. 7.4 Structure Members - cont’1 <ul><li>Example 7.8 </li></ul>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 customer’s */ /* account no. */ cust.accNo /* to access the customer’s */ /* name */ cust.name cust.name[2] &cust.name[2] /* to access the month of */ /* the payment of customer */ cust.payment.month
  19. 19. 7.5 Initializing Structures <ul><li>The members of a structure variable can be assigned initial values. </li></ul><ul><li>The initial values must appear in the order in which they will be assigned to their corresponding structure members. </li></ul>
  20. 20. 7.5 Initializing Structures – cont’1 <ul><li>The general form: </li></ul>storage_class struct tag variable = {value1, value2, ..., valueN}; Refer to the value of corresponding structure members auto, extern, register, static
  21. 21. 7.5 Initializing Structures – cont’1 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. 22. 7.5 Initializing Structures – cont’2 Example 7.10:
  23. 23. 7.6 Nested Stuctures <ul><li>saves time when writing programs that use similar structure. </li></ul><ul><li>The common members have to define only once in their own structure and then use that structure as a member in another structure. </li></ul>
  24. 24. 7.6 Nested Structures – cont’1 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. 25. 7.7 Arrays of Structures <ul><li>Example 7.14: Declaration of a 200-element array of structures. </li></ul>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. 26. 7.7 Arrays of Structures – cont’2 Example 7.16
  27. 27. 7.7 Arrays of Structures – cont’1 <ul><li>Example 7.15: Assigning initial values to an array of structures </li></ul>struct student { char name[80]; float courseMark; float finalMark; int TotMark; }; static struct student MarkList[ ] = { “ Kadir”, 45.50, 30.00, 76, &quot;Azizan&quot;, 43.50, 30.00, 73, &quot;Xavier&quot;, 44.50, 30.00, 75, &quot;Nantha&quot;, 46.50, 30.00, 77, &quot;Junani&quot;, 42.50, 30.00, 73, &quot;Martha&quot;, 42.00, 30.00, 72 } ;
  28. 28. 7.8 Structures and Pointers <ul><li>A pointer to a structure type variable is declared by a statement: </li></ul>struct name *ptr ; <ul><li>A structure member could be accessed by pointers with the following statements: </li></ul>struct PersonalData *ptr ; (*ptr).YearOfBirth=20 ; struct PersonalData *ptr ; ptr -> YearOfBirth=20 ; OR
  29. 29. 7.8 Structures and Pointers – cont’1 Example 7.16: Example 7.17:
  30. 30. 7.9 Passing Structures to Func <ul><li>There are several different ways to pass structure-type information to or from a function. </li></ul><ul><ul><li>transferred individually , or </li></ul></ul><ul><ul><li>entire structures can be transferred. </li></ul></ul>
  31. 31. 7.9 Passing Structures … – cont’1 <ul><li>Individual structure members can be: </li></ul><ul><ul><li>passed to a function as arguments in the function call, and </li></ul></ul><ul><ul><li>a single structure member can be returned by the return statement. </li></ul></ul><ul><li>Each structure member is treated the same as an ordinary single-valued variable. </li></ul>
  32. 32. 7.9 Passing Structures … – cont’2 <ul><li>Example 7.18: </li></ul><ul><ul><li>The program transfer partial structure member to a function, addition. </li></ul></ul>
  33. 33. 7.9 Passing Structures … – cont’3 <ul><li>Example 7.19: </li></ul><ul><ul><li>The program transfer a structure-type pointer to a function, adjust . </li></ul></ul>

×