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
Showing posts with label 9. STRUCTURES AND UNIONS. Show all posts
Showing posts with label 9. STRUCTURES AND UNIONS. Show all posts
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.
Monday, April 19, 2010
9.1 DEFINING A STRUCTURE
Structure declarations are a bit more complex than array declarations, since a structure must be defined in terms of its individual members.In general terms, the composition of a structure may be defined as
struct tag {
member 1;
member 2;
...............
member n;
};
In the above declaration struct is the required keyword; tag is the name of the structure; and member 1, member 2,......., member n are individual member declarations.(Note: there is no formal distinction between a structure definition and a structure declaration; the terms are used interchangeably.)
The member names must be distinct from one another within a particular structure, though a member name can be same as the name of a variable that is defined outside of the structure. A storage class, however cannot be assigned to an individual member, and individual members cannot be initialized within a structure type declaration.
Once the composition of the structure has been defined, individual structure-type variables can be declared as follows
storage-class struct tag variable 1,variable 2,............,variable n;
where storage-class is an optional storage class specifier, struct is a required keyword, tag is the name that appeared in the structure declaration, and variable 1,variable 2,............,variable n are structure variables of type tag.
It is possible to combine the declaration of the structure composition with that of the structure variables, as shown below.
storage-class struct tag {
member 1;
member 2;
...............
member n;
}variable 1,variable 2,............,variable n;
The tag is optional in this situation.
A C program contains 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;
} oldcustomer, newcustomer;
The second structure (account) now contains another structure (date) as one of its members.Note that declaration of date precedes the declaration of account. The following diagram explains the composition of account.
The assignment of initial values to the members of a structure variable are done as
static struct account customer = {12345, 'R', "John", 550.50, 5, 24, 90};
Thus, a customer is a static structure variable of type account, whose members are assigned initial values. The first member(acct_no) is assigned the integer value 12345,the second member(acct_type) is assigned the character 'R', the third member name[80] is assigned the string "John", and the fourth member(balance) is assigned the floating-point value 550.50. The last member is itself a structure that contains three integer members (month, day and year). Therefore, the last member of customer is assigned the integer values 5, 24 and 90.
struct tag {
member 1;
member 2;
...............
member n;
};
In the above declaration struct is the required keyword; tag is the name of the structure; and member 1, member 2,......., member n are individual member declarations.(Note: there is no formal distinction between a structure definition and a structure declaration; the terms are used interchangeably.)
The member names must be distinct from one another within a particular structure, though a member name can be same as the name of a variable that is defined outside of the structure. A storage class, however cannot be assigned to an individual member, and individual members cannot be initialized within a structure type declaration.
Once the composition of the structure has been defined, individual structure-type variables can be declared as follows
storage-class struct tag variable 1,variable 2,............,variable n;
where storage-class is an optional storage class specifier, struct is a required keyword, tag is the name that appeared in the structure declaration, and variable 1,variable 2,............,variable n are structure variables of type tag.
It is possible to combine the declaration of the structure composition with that of the structure variables, as shown below.
storage-class struct tag {
member 1;
member 2;
...............
member n;
}variable 1,variable 2,............,variable n;
The tag is optional in this situation.
A C program contains 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;
} oldcustomer, newcustomer;
The second structure (account) now contains another structure (date) as one of its members.Note that declaration of date precedes the declaration of account. The following diagram explains the composition of account.
The assignment of initial values to the members of a structure variable are done as
static struct account customer = {12345, 'R', "John", 550.50, 5, 24, 90};
Thus, a customer is a static structure variable of type account, whose members are assigned initial values. The first member(acct_no) is assigned the integer value 12345,the second member(acct_type) is assigned the character 'R', the third member name[80] is assigned the string "John", and the fourth member(balance) is assigned the floating-point value 550.50. The last member is itself a structure that contains three integer members (month, day and year). Therefore, the last member of customer is assigned the integer values 5, 24 and 90.
Friday, January 8, 2010
9.7 UNIONS
Like structures, unions also contain members whose individual data types may differ from one another.However, the members of a union share the same storage area within the computer's memory, whereas each members within a structure is assigned its own unique storage area.thus, unions are used to conserve memory.They are useful for applications involving multiple members, where values need not be assigned to all of the members at any one time.
In general terms, the composition of a union may be defined as
union tag {
member 1;
member 2;
...............
member n;
};
where union is a required keyword and other terms have the same meaning as in a structure definition. Individual union variables may be defined as
storage-class union tag variable 1,variable 2,............,variable n;
where storage-class is an optional storage class specifier, union is a required keyword, tag is the name that appeared in the structure declaration, and variable 1,variable 2,............,variable n are structure variables of type tag.
The two declarations can be combined, just as we did with structures. Thus, we can write
storage-class union tag {
member 1;
member 2;
...............
member n;
}variable 1,variable 2,............,variable n;
The tag is optional in this situation.
Let us consider the simple C program shown below.
#include < stdio.h >
main()
{
union id {
char color;
int size;
};
struct {
char manufacture[20];
float cost;
union id description;
} shirt, blouse;
printf("%d\n", sizeof(unionid));
/*assign a value to color*/
shirt.description.color = 'w';
printf("%c %d\n", shirt.description.color, shirt.description.size);
/*assign a value to size*/
shirt.description.size = 12;
printf("%c %d\n", shirt.description.color, shirt.description.size);
}
Execution of the program results n the following output.
2
w -24713
@ 12
The first line indicates that the union is allocated two bytes of memory, to accommodate an integer quantity.In line 2, the first data item (w) is meaningful, but the second (-24713) is not.In line 3, the first data item (@)is meaningless, but the second data (12) item has a meaning.Thus, each data item has a meaningful value in accordance with the assignment statement preceding each printf statement.
In general terms, the composition of a union may be defined as
union tag {
member 1;
member 2;
...............
member n;
};
where union is a required keyword and other terms have the same meaning as in a structure definition. Individual union variables may be defined as
storage-class union tag variable 1,variable 2,............,variable n;
where storage-class is an optional storage class specifier, union is a required keyword, tag is the name that appeared in the structure declaration, and variable 1,variable 2,............,variable n are structure variables of type tag.
The two declarations can be combined, just as we did with structures. Thus, we can write
storage-class union tag {
member 1;
member 2;
...............
member n;
}variable 1,variable 2,............,variable n;
The tag is optional in this situation.
Let us consider the simple C program shown below.
#include < stdio.h >
main()
{
union id {
char color;
int size;
};
struct {
char manufacture[20];
float cost;
union id description;
} shirt, blouse;
printf("%d\n", sizeof(unionid));
/*assign a value to color*/
shirt.description.color = 'w';
printf("%c %d\n", shirt.description.color, shirt.description.size);
/*assign a value to size*/
shirt.description.size = 12;
printf("%c %d\n", shirt.description.color, shirt.description.size);
}
Execution of the program results n the following output.
2
w -24713
@ 12
The first line indicates that the union is allocated two bytes of memory, to accommodate an integer quantity.In line 2, the first data item (w) is meaningful, but the second (-24713) is not.In line 3, the first data item (@)is meaningless, but the second data (12) item has a meaning.Thus, each data item has a meaningful value in accordance with the assignment statement preceding each printf statement.
9.6 SELF-REFERNTIAL STRUCTURES
It is some times desirable to include within a structure one member that is a pointer to the parent structure type.In general terms, this can be expressed as
struct tag {
member 1;
member 2;
..............
struct tag *name;
};
where name refers to the name of a pointer variable.Thus the structure of type tag will contain a member that points to another structure of type tag. Such structures are known as self-referential structures.
Self referential structures are very useful in applications that involve linked data structures, such as lists and trees.
The basic idea of linked data structure is that each component within the structure includes a pointer indicating where the next component can be found.Therefore, the relative order of the components can easily be changed simply by altering the pointers.In addition, individual components can easily be added or deleted, again by altering the pointers. As a result, a linked data structure is not confined to some maximum number of components.Rather, the data structure can expand or contract in size as required.
Fig. 1 illustrates a linked list containing three components.Each component consists of two data items; a string and a pointer that references the next component within the list.Thus, the first component contains the string red, the second contains green and the third contains blue.Also, the end of the list is indicated by a special pointer, called NULL.
Now let us add another component, whose value is white, between red and green. To do so, we merely change the pointers as illustrated in fig. 2. Similarly, if we choose to delete the component whose value is green, we simply change the pointer associated with the second component, as shown in fig. 3.
In fig. 4 we see an example of tree. Trees consists of nodes and branches, arranged in some hierarchical manner which indicates a corresponding hierarchical structure within the data.(A binary tree is a tree in which every node has no more than two branches.)
In fig. 4 the root node has the value screen, and the associated branches lead to the nodes whose values are foreground and background, respectively. Similarly, the branches associated with foreground lead to the nodes whose values are white, green and amber, and the branches associated with background lead to the nodes whose values are black,blue and white.
Self referential structures are ideally suited for applications involving linked data structures.Each structure will represent a single component (i.e. one node) within the linked data structure.The self referential pointer will indicate the location of the next component.
struct tag {
member 1;
member 2;
..............
struct tag *name;
};
where name refers to the name of a pointer variable.Thus the structure of type tag will contain a member that points to another structure of type tag. Such structures are known as self-referential structures.
Self referential structures are very useful in applications that involve linked data structures, such as lists and trees.
The basic idea of linked data structure is that each component within the structure includes a pointer indicating where the next component can be found.Therefore, the relative order of the components can easily be changed simply by altering the pointers.In addition, individual components can easily be added or deleted, again by altering the pointers. As a result, a linked data structure is not confined to some maximum number of components.Rather, the data structure can expand or contract in size as required.
Fig. 1 illustrates a linked list containing three components.Each component consists of two data items; a string and a pointer that references the next component within the list.Thus, the first component contains the string red, the second contains green and the third contains blue.Also, the end of the list is indicated by a special pointer, called NULL.
Now let us add another component, whose value is white, between red and green. To do so, we merely change the pointers as illustrated in fig. 2. Similarly, if we choose to delete the component whose value is green, we simply change the pointer associated with the second component, as shown in fig. 3.
In fig. 4 the root node has the value screen, and the associated branches lead to the nodes whose values are foreground and background, respectively. Similarly, the branches associated with foreground lead to the nodes whose values are white, green and amber, and the branches associated with background lead to the nodes whose values are black,blue and white.
Fig. 5 explains the manner in which pointers are used to construct tree.
Tuesday, January 5, 2010
9.5 PASSING STRUCTURES TO FUNCTIONS
There are many different ways to pass structure-type information to or from a function.Structure members can be transferred individually or the whole structure can be passed to a function.
Individual structure members can be passed to a function as arguments in the function call, a single structure member can be returned via the return statement.To do so, each structure member is treated the same as an ordinary single-valued variable.
Consider the simple C program shown below.
#include < stdio.h >
typedef struct {
int acct_no;
char acct_type;
char *name;
float balance;
} record;
main() /*transfer a structure-type pointer to a function*/
{
void adjust(record *pt); /*function declaration*/
static record customer = {"Smith", 5120, 'R', 100.35};
printf("%s %d %c %.2f\n",customer.name, customer.acct_no, customer.acct_type, customer.balance);
adjust(&customer);
printf("%s %d %c %.2f\n",customer.name, customer.acct_no, customer.acct_type, customer.balance);
}
void adjust (record *pt) /*function definition*/
{
pt->name = "Jones";
pt->acct_no = 5635;
pt->acct_type = 'C';
pt->balance =200.65;
return;
}
This program illustrates the transfer of a structure to a function by passing the structure's address(a pointer) to the function.in particular, customer is a static structure of type record, whose members are assigned an initial set of values.These initial values are displayed when the program begins to execute.The structure's address is then passed to the function adjust, where different values are assigned to the members of the structure.
Execution of the program results in the following output.
Smith 5120 R 100.35
Jones 5635 C 200.65
Thus the values assigned to the members of customer within adjust are recognized within main, as expected.
Individual structure members can be passed to a function as arguments in the function call, a single structure member can be returned via the return statement.To do so, each structure member is treated the same as an ordinary single-valued variable.
Consider the simple C program shown below.
#include < stdio.h >
typedef struct {
int acct_no;
char acct_type;
char *name;
float balance;
} record;
main() /*transfer a structure-type pointer to a function*/
{
void adjust(record *pt); /*function declaration*/
static record customer = {"Smith", 5120, 'R', 100.35};
printf("%s %d %c %.2f\n",customer.name, customer.acct_no, customer.acct_type, customer.balance);
adjust(&customer);
printf("%s %d %c %.2f\n",customer.name, customer.acct_no, customer.acct_type, customer.balance);
}
void adjust (record *pt) /*function definition*/
{
pt->name = "Jones";
pt->acct_no = 5635;
pt->acct_type = 'C';
pt->balance =200.65;
return;
}
This program illustrates the transfer of a structure to a function by passing the structure's address(a pointer) to the function.in particular, customer is a static structure of type record, whose members are assigned an initial set of values.These initial values are displayed when the program begins to execute.The structure's address is then passed to the function adjust, where different values are assigned to the members of the structure.
Execution of the program results in the following output.
Smith 5120 R 100.35
Jones 5635 C 200.65
Thus the values assigned to the members of customer within adjust are recognized within main, as expected.
9.4 STRUCTURES AND POINTERS
The address(&) operator can be used to access the beginning address of a structure, in the same manner as any other address.Thus, if variable represents a structure type variable, then &variable represents the starting address of that variable.We can declare a pointer variable for a structure by writing
type *ptvar;
where type is a data type that identifies the composition of the structure, and ptvar represents the name of the pointer variable.The beginning address of a structure can be assigned to this pointer by writing
ptvar = &variable;
The variable declaration and pointer declaration can be combined with the structure declaration by writing
struct {
member 1;
member 2;
...............
member n;
} variable, *ptvar;
An individual member of a structure can be accessed in terms of its corresponding pointer variable by writing
ptvar->member;
which is equivalent to writing
variable.member;
The operator -> is comparable to the period operator (.).
Consider the simple C program shown below,
#include < stdio.h >
main()
{
int n=5120;
char t = 'R';
float b = 100.35;
typedef struct {
int month;
int day;
int year;
} date;
struct {
int *acct_no;
char *acct_type;
char *name;
float *balance;
date lastpayment;
} customer, *pc = &customer;
customer.acct_no = &n;
customer.acct_type = &t;
customer.name = "Smith";
customer.balance = &b;
printf("%d %c %s %.2f\n", *customer.acct_no, *customer.acct_type, customer.name, *customer.balance);
printf("%d %c %s %.2f\n", *pc->acct_no, *pc->acct_type, pc->name, pc->balance);
}
The members acct_no, acct_type, name, balance are written as pointers within the second structure.Thus, the value to which acct_no points can be accessed by writing either
*customer.acct_no or *pc->acct_no.The same way other members can also be accessed.
Execution of the program results in the following two lines of output.
5120 R Smith 100.35
5120 R Smith 100.35
These two lines of output are identical, as expected.
type *ptvar;
where type is a data type that identifies the composition of the structure, and ptvar represents the name of the pointer variable.The beginning address of a structure can be assigned to this pointer by writing
ptvar = &variable;
The variable declaration and pointer declaration can be combined with the structure declaration by writing
struct {
member 1;
member 2;
...............
member n;
} variable, *ptvar;
An individual member of a structure can be accessed in terms of its corresponding pointer variable by writing
ptvar->member;
which is equivalent to writing
variable.member;
The operator -> is comparable to the period operator (.).
Consider the simple C program shown below,
#include < stdio.h >
main()
{
int n=5120;
char t = 'R';
float b = 100.35;
typedef struct {
int month;
int day;
int year;
} date;
struct {
int *acct_no;
char *acct_type;
char *name;
float *balance;
date lastpayment;
} customer, *pc = &customer;
customer.acct_no = &n;
customer.acct_type = &t;
customer.name = "Smith";
customer.balance = &b;
printf("%d %c %s %.2f\n", *customer.acct_no, *customer.acct_type, customer.name, *customer.balance);
printf("%d %c %s %.2f\n", *pc->acct_no, *pc->acct_type, pc->name, pc->balance);
}
The members acct_no, acct_type, name, balance are written as pointers within the second structure.Thus, the value to which acct_no points can be accessed by writing either
*customer.acct_no or *pc->acct_no.The same way other members can also be accessed.
Execution of the program results in the following two lines of output.
5120 R Smith 100.35
5120 R Smith 100.35
These two lines of output are identical, as expected.
Monday, January 4, 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.
9.2 PROCESSING A STRUCTURE
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
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
Sunday, January 3, 2010
9. STRUCTURE AND UNIONS
We have already discussed about array, which is a data structure whose elements are of same data type.
We will now discuss about structure, in which the individual elements may differ in type.The individual structure elements are referred as members.
Unions resembles closely with structures,which also can have multiple members. Difference between structures and union is that unlike structures members of a union share the same storage area, even though the individual members may differ in type.
We will now discuss about structure, in which the individual elements may differ in type.The individual structure elements are referred as members.
Unions resembles closely with structures,which also can have multiple members. Difference between structures and union is that unlike structures members of a union share the same storage area, even though the individual members may differ in type.
Subscribe to:
Posts (Atom)