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.
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