UNIX C Programming

Chapter 14: Structures and Other Data Forms



This chapter introduces the concept of structures and how to create structure templates and variables. The objectives important to this chapter are to:

  • Introduce the concept of a structure
  • Explain the difference between a structure template and a structure variable
  • Clarify how a structure can be passed to a function
  • Explain how members of a structure can be referenced
  • Introduce the concept of an array of structures
  • Explain how an array of structures is passed to a function
  • Show how a union differs from a typedef
  • Discuss the use of typedef

A structure provides the means to store several data items, usually of different types,in the same data object.

A structure template is the master plan that describes how a structure is put together. The format for a template is as follows:

	struct tag 	{
/*The tag is optional, but almost always used */
	type member1;	
	type member2;	
	type member3;

A structure is declared by adding a variable name to the template. The general form is:

	struct tag variable_name;

A structure variable can be of the tag structuretype, or a pointer to the tag structuretype.

Access to a structure type uses the dot (.) operator, as in:


An array of structures is like any other kind of array. An example is as follows:

	struct book libry[MAXBKS];

Apply the same rules to an array of structures as to other types of arrays. Use the dot operator to access a member. For example,

/*  meaning the member called 
	title in libry[4] */ 

It is possible to nest structures. A member of a structure can be of type struct.

Pointers to structures are declared as follows:

	struct guy * him;  
/* him is pointer to a structure of "type" guy */
/* note that this is a declaration, 
	not an initialization */

where the template name, guy, is followed by a star, followed by the pointer name.

Member access by pointer is a difficult concept for some to understand. Suppose him is a pointer to a structure fellow[0]. This being the case, the following equalities exist:

fellow[0].income == (* him).income == him -> income

In this example, the -> operator is called the indirect membership operator. The last expression is described as follows:

income is a member of the pointed-to structure

Structures can be passed to functions in three ways: by passing structures as arguments, by passing pointers to structures as arguments, and by passing structure members as arguments.

When the address of a structure is passed to a function, the indirect membership operator ( ->) is required to gain access to a member. When the structure is passed to a function, the dot operator (.) isrequired to gain access to a member.

The advantages of the pointer argument method are that it works with old and new versions of C, is quick, and saves storage space. The advantages of passing the structure is that a copy of the structure is passed. This is safer and often easier to understand.

A union is a special type of structure. It stores different data types in the same memory space, but not simultaneously. A union must allocate space for the largest type in the union template.

The typedef function allows you to create your own name for a type. The format for a typedef is

typedef type symbolic_name;

Reasons for using typedef include providing reasonable names for data types and for complicated types, and making a program more portable.

A function pointer is also found in the C language. It is often used as an argument to another function, telling the second function which functions to use. An example of a function pointer prototype is:

int (*fp) (const char * ps);

In this example, fp points to a function that takes a pointer to char as an argument and returns an int.