Lecture 20
The
values of a structure variable can be assigned to another structure variable of
the same type using the assignment operator. It is not necessary to copy the
structure elements piece-meal. Obviously, programmers prefer assignment to
piece-meal copying. This is shown in the following example.
Ability to copy the
contents of all structure elements of one variable into the corresponding
elements of another structure variable is rather surprising, since C does not
allow assigning the contents of one array to another just by equating the two.
As we saw earlier, for copying arrays we have to copy the contents of the array
element by element.
This copying of all
structure elements at one go has been possible only because the structure
elements are stored in contiguous memory locations. Had this not been so, we
would have been required to copy structure variables element by element. And
who knows, had this been so, structures would not have become popular at all.
One structure can be
nested within another structure. Using this facility complex data types can be
created. The following program shows nested structures at work.
Notice the method
used to access the element of a structure that is part of another structure.
For this the dot operator is used twice, as in the expression,
e.a.pin or e.a.city
Of course, the
nesting process need not stop at this level. We can nest a structure within a
structure, within another structure, which is in still another structure and so
on... till the time we can comprehend the structure ourselves. Such
construction however gives rise to variable names that can be surprisingly self
descriptive, for example:
maruti.engine.bolt.large.qty
This clearly signifies that we are
referring to the quantity of large sized bolts that fit on an engine of a
maruti car.
Like an ordinary
variable, a structure variable can also be passed to a function. We may either
pass individual structure elements or the entire structure variable at one go.
Let us examine both the approaches one by one using suitable programs.
Observe that in the
declaration of the structure, name and author have been declared
as arrays. Therefore, when we call the function display( ) using,
display ( b1.name,
b1.author, b1.callno ) ;
we are passing the base addresses of the
arrays name and author, but the value stored in callno. Thus, this is a mixed
call—a call by reference as well as a call by value.
It can be immediately realized that to
pass individual elements would become more tedious as the number of structure
elements go on increasing. A better way would be to pass the entire structure
variable at a time. This method is shown in the following program.
Note that here the
calling of function display( ) becomes quite compact,
display
( b1 ) ;
Having collected what
is being passed to the display( ) function, the question comes, how do
we define the formal arguments in the function. We cannot say,
struct book b1 ;
because the data type
struct book is not known to the function display( ). Therefore,
it becomes necessary to define the structure type struct book outside main(
), so that it becomes known to all functions in the program.
The way we can have a
pointer pointing to an int, or a pointer pointing to a char,
similarly we can have a pointer pointing to a struct. Such pointers are
known as ‘structure pointers’. Let us look at a program that demonstrates the
usage of a structure pointer.
The first printf(
) is as usual. The second printf( ) however is peculiar. We can’t
use ptr.name or ptr.callno because ptr is not a structure
variable but a pointer to a structure, and the dot operator requires a
structure variable on its left. In such cases C provides an operator ->,
called an arrow operator to refer to the structure elements. Remember that on
the left hand side of the ‘.’ structure operator, there must
always be a structure variable, whereas on the left hand side of the ‘->’
operator there must always be a pointer to a structure. The arrangement of
the structure variable and pointer to structure in memory is shown-
Can we not pass the
address of a structure variable to a function? We can. The following program
demonstrates this.
Again note that to
access the structure elements using pointer to a structure we have to use the ‘->’
operator. Also, the structure struct book should be declared outside
main( ) such that this data type is available to display( ) while
declaring pointer to the structure.
1 comments:
This is great blog! Keep it up. aircon chemical wash singapore
Post a Comment