There are many objects defined throughout the library as structures of varying and possibly large sizes. Due to the inefficiency of passing structures by value (copying the whole structure to a function), structures are generally passed to functions by reference. Because of this, there is always a potential to pass an invalid pointer to a function which expects a pointer to a structure. The following examples illustrate the right and wrong ways to use such a function. Given a structure and a library function which initializes it,
typedef struct
{
int a, b, c, d;
} big_struct;
void initialize( struct big_struct *s )
{
s->a = 1; s->b = 2; s->c = 3; s->d = 4;
}
the incorrect method of using this function is:
int main()
{
big_struct *s;
initialize( s ); /* WRONG */
}
because the variable s is an uninitialized pointer. The
correct method is to define a structure variable, not a pointer to a
structure, and pass a pointer to the structure:
int main()
{
big_struct s;
initialize( &s );
}
Alternately, the incorrect example above could have been corrected by
allocating the s pointer before calling initialize.