Next: Further Uses of Up: Structures in C Previous: Accessing Members of

Structures as Function Arguments

A structure can be passed as a function argument just like any other variable. This raises a few practical issues.

Where we wish to modify the value of members of the structure, we must pass a pointer to that structure. This is just like passing a pointer to an int type argument whose value we wish to change.

If we are only interested in one member of a structure, it is probably simpler to just pass that member. This will make for a simpler function, which is easier to re-use. Of course if we wish to change the value of that member, we should pass a pointer to it.

When a structure is passed as an argument, each member of the structure is copied. This can prove expensive where structures are large or functions are called frequently. Passing and working with pointers to large structures may be more efficient in such cases.
Tue Jan 17 11:40:37 GMT 1995