Defining a Structure Variable

I l @ ve RuBoard

Defining a Structure Variable

The word structure is used in two senses. One is the sense "structure plan," which is what we just discussed. The structure plan tells the compiler how to represent the data, but it doesn't make the computer allocate space for the data. The next step is to create a "structure variable," the second sense of the word. The line in the program that causes a structure variable to be created is this:

 struct book libry; 

Seeing this instruction, the compiler creates the variable libry . Using the book template, the compiler allots space for a char array of MAXTITL elements, for a char array of MAXAUTL elements, and for a float variable. This storage is lumped together under the single name libry (see Figure 14.1). (The next section explains how to unlump it as needed.)

Figure 14.1. Memory allocation for a structure.
graphics/14fig01.jpg

In declaring a structure variable, struct book plays the same role that int or float does in simpler declarations. For example, you could declare two variables of the struct book type or even a pointer to that kind of structure:

 struct book doyle, panshin, * ptbook; 

The structure variables doyle and panshin would each have the parts title , author , and value . The pointer ptbook could point to doyle , panshin , or any other book structure. In essence, the book structure declaration creates a new type called struct book .

As far as the computer is concerned , the following declaration

 struct book libry; 

is short for

 struct book {    char title[MAXTITL];    char author[AXAUTL];    float value; }  libry;    /* follow declaration with variable name */ 

In other words, the process of declaring a structure and the process of defining a structure variable can be combined into one step. Combining the declaration and the variable definitions, as shown here, is the one circumstance in which a tag need not be used:

 struct {         /* no tag */     char title[MAXTITL];     char author[MAXAUTL];     float value; } libry; 

Use the tag form, however, if you plan to use a structure template more than once. There is one aspect of defining a structure variable that did not come up in this example: initialization. We'll look at that now.

Initializing a Structure

You've seen how to initialize variables and arrays:

 int count = 0; int fibo[7] = {0,1,1,2,3,5,8}; 

Can a structure variable be initialized , too? Yes, although many pre-ANSI implementations limit initialization to external or static structure variables. Whether a structure variable is external depends on where the variable is defined, not on the location of the declaration describing the structure layout. In the example, the declaration for the book structure is external, but the variable libry is not because it is defined inside the function and is, by default, placed in the automatic storage class.

Similarly, the declaration

 static struct book libry; 

would create a structure with the static storage class.

To initialize a structure (any storage class for ANSI C, but excluding automatic variables for pre-ANSI C), you use a syntax similar to that used for arrays:

 struct book libry = {     "The Pirate and the Damsel",     "Renee Vivotte",     1.95 }; 

In short, you use a comma-separated list of initializers enclosed in braces. Each initializer should match the type of structure member being initialized. Therefore, you can initialize the title member to a string and the value member to a number. To make the associations more obvious, we gave each member its own line of initialization, but all the compiler needs are commas to separate one member's initialization from the next.

Now, let's continue with structure properties.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net