Vous êtes sur la page 1sur 4

In this chapter you will learn about -

-->Structures and Unions,


-->Giving values to members,
-->Initializing structure,
-->Functions and structures,
-->Passing structure to elements to functions,
-->Passing entire function to functions,
-->Arrays of structure,
-->Structure within a structure
-->self/Forward-Referencing Structures
--> Union.
Structure:
** A structure is a collection of variables referenced under one name.
**Combine variables into a single package called a structure.
**Structure contains a number of datatype grouped.
**Structures are declared by using the struct keyword.
structure definition:
general format:
struct tag_name
{
data type member1;
data type member2;

}
Example:
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
-->The keyword struct declares a structure to holds the details of four fields
namely title, author pages and price.
-->We can declare structure variables using the tag name any where in the
program.
struct lib_books book1,book2,book3;
-->We can also combine both template declaration and variables declaration
in one statement, the declaration
struct
{
char title[20];
char author[15];
int pages;
float price;
} book1,book2,book3;
Initializing structure:
Struct lib_books
{ C language,E.Balagurusamy,289,200.00};
Referencing Structure Members with the Dot Operator
gets(book1.title);
gets(book1.author)
scanf("%d",&book1.pages);
scanf("%f",&book1.price);
/*example 1 Referencing the members of a structure*/
/*example 2 Initializing a structure */
Arrays of structure:
-->It is possible to define a array of structures
structure information
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
student[100];
-->An array of structures can be assigned initial values just as any other
array can.
-->Remember that each element is a structure that must be assigned
corresponding initial values
The sturucture student constains another structure date as its one of its
members.
Example 3 :
Functions and structures:
--> We can pass structures as arguments to functions.
--> Passing structure to elements to function
-->We can return structure from functions
A structure may be passed into a function as individual member or a separate
variable.
example 4:
Nested Structures
In C, we can use a structure as a member of another structure. When a structure
is declared
as member of another structure, it is called a nested structure or structure
within a structure.
example:
struct date
{
int day,month,year;
};
struct student
{
char name[20],sex;
int rollno;
struct date dob;
};
To process the individual elements in a nested structure, first we have to
declare the structure variable of the date structure(dob) and then its
individual fields.
Functions
Example 5
Pointing to Structures
As you can pass a function with a pointer that refers to an array, you can also
pass a function with a pointer that points to a
structure.
However, unlike passing a structure to a function, which sends an entire copy of
the structure to the function, passing a
pointer of a structure to a function is simply passing the address that associat
es the structure to the function. The function can
then use the address to access the structure members without duplicating the str
ucture. Therefore, it's more efficient to pass a
pointer of a structure, rather than the structure itself, to a function.
/*Example 6 pointer to structure variables */
self/Forward-Referencing Structures
If one of the members of a structure is a pointer pointing to another structure
that has not been declared yet, the structure is
called a forward-referencing structure.
For example, the following statement declares a forward-referencing structure:
struct x {
int i;
char ch[8];
struct y *ptr;
};
It is presumed that the structure y has not been declared yet.
If the pointer in a structure points to the structure itself, the structure is c
alled a self-referencing structure. The following
declaration is an example of a self-referencing structure:
struct x {
int i;
char ch[8];
struct x *ptr;
};
example 7
Union:
A union is a block of memory that is used to hold data items of different types.

In C, a union is similar to a structure, except that data items saved in the uni
on are
overlaid in order to share the same memory location. More details on the differe
nces
between unions and structures are discussed in the following sections.
-->Unions like structure contain members whose individual data types may
differ from one another.
-->The members that compose a union all share the same storage area within
the computers memory
-->They are useful for application involving multiple members. Where
values need not be assigned to all the members at any one time.
union item
{
int m;
float p;
char c;
}
code,*c;
-->we can use only one of them at a time.
-->This is because if only one location is allocated for union variable
irrespective of size.
Referring a Union with . or ->
syntax that we use to access structure members.
code.m
code.p
code.c
c=code;
c->m
c->p
Example 8 :
The Size of a Union
You've been told that the members of a union share the same memory location. The
size of a union is the same as the size of
the largest member in the union

Vous aimerez peut-être aussi