Mail Archives: djgpp/1998/08/13/14:01:46
Bjorn Hansen wrote:
>
> Is it possible to pass a structure to a function ? If so how would I
> protorype it?
>
> Bjorn Hansen
Yes.
Suppose you have a struct XX:
struct XX
{
int a, b
};
You can pass the whole structure by value. In this case you will be
working
on a copy of the original structure and not on the original itself. This
is
also slower than passing the address of the structure (in case of a big
data
structure).
voif f1( struct XX x )
{
/* ... */
}
The common convention is to pass the address of the structure
by a pointer variable. If the structure is read-only inside
the function (i.e. the function only retrieve data but not
modify the structure) then use a const pointer.
void f_mutating( struct XX* px )
{
/* Note that the pointer is not const. We can read and write */
if ( px )
{
px->a = 2;
/* ... */
}
}
void f_const( const struct XX* px )
{
/*
* Note that the pointer is const. we can only read values.
*/
if (px)
printf("%d %d\n", px->a, px->b);
}
HTH
Eyal
- Raw text -