| Programmer's Reference for the BIC Volume IO Library | ||
|---|---|---|
| <<< Previous | Style | Next >>> |
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,
Example 1. The correct method of using this function:
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;
} |
Example 2. The incorrect method of using this function:
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:
Alternately, the incorrect example above could have been corrected by allocating the s pointer before calling initialize.
| <<< Previous | Home | Next >>> |
| Identifier Names | Up | Types and Macros |