Usually the members of structure are processed individually, as separate entities.A structure member can be accessed by writing
variable.member;
where variable refers to the name of a structure-type variable, and member refers to the name of the member within the structure.Notice the period (.) that separates the variable name from the member name.This period is an operator;it is a member of highest precedence group, and its associativity is left to right.
Let us consider the following structure declarations.
struct date {
int month;
int day;
int year;
};
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
struct date lastpayment;
} customer;
Here customer is a structure variable of type account.If we are to access customer's account number, we would write
customer.acct_no
Similarly, the customer's name and the customer's balance can be accessed by writing
customer.name
and
customer.balance
Thursday, June 24, 2010
9.3 USER-DEFINED DATA TYPES (typedef)
The typedef feature allows users to define new data-types that are equivalent to existing data types.
In general terms, a new data type is defined as
typedef type new-type;
where type refers to an existing data type (either a standard data type, or previous user-defined data type ), and new-type refers to the new user defined data type.It should be understood that the new data type will be new only in terms of name but in reality it is fundamentally same as the standard data types.
Here is a simple declaration involving the use of typedef.
typedef int age;
In this declaration age is a user defined data type, which is equivalent to type int.
Hence, the variable declaration
age male, female;
is equivalent to
int male, female;
The typedef is particularly convenient when defining structures, since it eliminates the need to write the struct tag whenever a structure is referenced.
Let us now look at the following declarations
typedef struct {
int acct_no;
char acct_type;
char name[80];
float balance;
} record;
record oldcustomer, newcustomer;
The first declaration defines record as a user defined data type.The second declaration defines old customer and new customer as structure variables of type record.
In general terms, a new data type is defined as
typedef type new-type;
where type refers to an existing data type (either a standard data type, or previous user-defined data type ), and new-type refers to the new user defined data type.It should be understood that the new data type will be new only in terms of name but in reality it is fundamentally same as the standard data types.
Here is a simple declaration involving the use of typedef.
typedef int age;
In this declaration age is a user defined data type, which is equivalent to type int.
Hence, the variable declaration
age male, female;
is equivalent to
int male, female;
The typedef is particularly convenient when defining structures, since it eliminates the need to write the struct tag whenever a structure is referenced.
Let us now look at the following declarations
typedef struct {
int acct_no;
char acct_type;
char name[80];
float balance;
} record;
record oldcustomer, newcustomer;
The first declaration defines record as a user defined data type.The second declaration defines old customer and new customer as structure variables of type record.
Subscribe to:
Comments (Atom)