Return to sysprog.net home page

españolfrançaisdeutsch

C and C++

Main page

Defines

Design

Loops

Pointers

Punctuation

Strings

Stuctures

Types


SITE SERVICES
Contact Us
Search
Site Map

Home —» C and C++> —» structures



C Tutorial: Structures


Defining Structures:
C structures are used to defines groups of contiguous fields, such as records or control blocks. Usually, the structure definition is saved as an #include member in a maclib library, but it can be placed at the top of the program. The commonest way to define structures is with a typedef, as shown below.

typedef struct country
   {
   char     name[20];
   int    population;
   char language[10];
   } Country;

This defines a structure which can be referred to either as 'struct country' or Country, whichever you prefer. Strictly speaking, you don't need a tag name both before and after the braces if you're not going to use one or the other. But it's standard practice to put them both in and to give them the same name, but with the one after the braces starting with an uppercase letter.

The typedef statement doesn't occupy storage: it simply defines a new type. Variables that are declared with the typedef above will be of type 'struct country', just like population is of type integer.

By default, C will align arithmetic fields on halfword, fullword or doubleword boundaries, depending on the type. You can force the compiler to eliminate slack bytes caused by boundary alignment by using compiler options, such as pack in C/370 or bytealign in SAS/C.

Declaring Structures:
Once the structure is defined, you can declare a structure variable by preceding the variable name by the structure type name. If you've defined the structure with tags both before and after the braces, it's a matter of style whether you declare the variable using the word struct or not. Both the following lines declare variables of the type defined above.

struct country France, Italy, Sweden;
Country Mexico, Canada, Brazil;

If you have a small structure you just want to define in the program, you can do the definition and declaration together as shown below. This will define a structure of type 'struct country' and declare three instances of it.

struct country
   {
   char     name[20];
   int    population;
   char language[10];
   } India, Singapore, Indonesia;

Strictly speaking, the tag name before the brace isn't needed here, but it's usually left in to comment the structure. You can also use it to declare new variables, just as with typedef.

struct country Australia;

Addressing a Field in a Structure:
Fields in a structure can be referenced by following the name of the structure by a dot and the name of the field.

France.population = 0;

You can also declare a pointer to type 'struct country' and use that to reference the fields in a structure of that type. In this case, you follow the pointer name by an arrow and the name of the field.

struct country Russia, *sptr;
sptr = &Russia;
sptr->population = 0;

In the example above, Russia is of type 'struct country'; *sptr is of type 'pointer to struct country'; and sptr->population is of type integer.

Comparing and Copying Structures:
Individual fields in structures can be compared or copied like any other variables.

Italy.population = France.population;

if (memcmp(Canada.language, France.language. sizeof(Canada.language)) == 0);
   {
   Translate(FRENCH);
   }
else;

The following line copies the entire structure of Mexico to Brazil.

memcpy(&Brazil, &Mexico, sizeof(Brazil));

If you're comparing one structure with another, all the usual warnings about slack bytes apply as in other languages.

Using Unions:
You can use unions to map different structures (or scalar variables) to the same area of memory. The syntax for unions is almost identical to that of structures.

Comparing C Structures to Java Classes:
You can read how Java classes compare to C structure in Chapter 5 of Effective Java, available on Sun Microsystems' web site.


TOP OF PAGE In My Egotistical Opinion, most people's C programs should be indented
six feet downward and covered with dirt.
(Blair P Houghton)