Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
92
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Part I • Honing Your C Skills

Listing 6.14. continued

$(LINK) @<< $(SRCS:.c=.obj) $(OBJS), $(NAME).exe, $(NAME).map,

$(LIBS), $(NAME).def

<<

!if $(DEBUG) !if !$(C7)

cvpack -p $(NAME).exe !endif

mapsym $(NAME).map !endif

##### Dependents #####

$(SRCS:.c=.obj): $(INCLS)

##### Clean Directory #####

clean:

-del *.obj -del *.exe

This example of a MAKE file does little more than the first, simpler example, but it does have the capability to quickly add new source (.C) files, to switch between debug mode and a final production version, and to handle Microsoft C 7’s differences.

In all, MAKE is one of the most important tools you have to help you produce your program. Without it, you have to do most of the work in creating your program, such as calling the compiler and linker.

Summary

This chapter described programs made up of more than one source file and how to manage larger, multisource file projects.

• The compiler is used to compile each source file.

186

Separate Compilation and Linking

C C C

 

C6C

 

C C C

 

C

When all the source files are compiled (successfully), they are combined, using a linker, to produce the final executable program.

The #include statement causes the C compiler to read in the named file as though it were part of the original file.

When the included file ends, the compiler continues with the original file.

External variables, identified with the extern keyword, can be used to share information between functions, even when the functions reside in different source files.

The object library utility (LIB) is used to maintain library files.

MAKE files are used to help automate the process of creating a large program that has more than one source file.

187

Part I • Honing Your C Skills

188

Part II

Managing Data in C

189

Advanced C

190

C CStructuresCC C C

C7C C

7 C C C

C C C

C C C

C Structures

A computer language would be ineffective if it did not offer a way to create complex data objects. C structures are objects that contain more than one item. A structure often contains data objects grouped according to their usage, but a structure can contain unrelated data objects as well.

Using the struct Keyword

You use the struct keyword to define a structure. A structure definition consists of several parts, as the following shows:

struct tag_name {

type member_name; type member_name; type member_name;

} structure_name = {initializer_values};

191

Part II • Managing Data in C

Although the formatting is up to the programmer, I suggest that you use the preceding format for easy readability.

The first line contains the struct keyword, then the optional tag_name:

struct tag_name {

The tag_name can be used to create a copy of the structure (as shown in STRUCT4.C, one of the example programs in this chapter). An opening brace follows the tag_name (or the struct keyword, if the tag_name is not used). This brace signals to the compiler that the next lines are member definitions. Each member definition consists of a variable type and a name. The members can be any valid variable type, including arrays, structures, and unions, as follows:

type member_name; type member_name; type member_name;

Followingthelastmembernameis aclosingbraceandtheoptionalstructure_name, as follows:

} structure_name =

When using the structure_name and the tag_name, you can choose any of the following:

If a structure_name is not specified and a tag_name is specified, the structure is being defined but not declared.

If a structure_name is specified and a tag_name is not specified, the structure is being declared but not defined.

If a structure_name and a tag_name are provided, the structure is being both defined and declared.

If neither a structure_name nor a tag_name is provided, a compile-time error will result.

If you want to initialize the structure, you must have a structure_name because it signals the compiler that this is a declaration. The structure_name is also necessary if you want to refer to the structure.

After the structure_name are optional initializers:

{initializer_values};

192

C Structures

C C C

 

C7C

 

C C C

 

C

The following is a simple structure:

struct

 

{

 

 

 

char

szSaying[129];

 

int

nLength;

}

MySaying;

 

This structure definition provides a data object that can be referenced with a single name, MySaying. Each member of MySaying provides different information.

Structures offer us a number of important advantages, including the following:

You can refer to the entire data object using a single name.

You can use the structure name as a parameter to a function. For example, you could pass the address and the length of the structure name to read() to read the structure’s contents from a disk file.

Structures can be assigned directly. You cannot assign strings (you must use the strcpy() library function), but you can assign two structures simply by using an assignment statement.

A function can return a structure.

A simple program that allocates and initializes a structure is shown in Listing 7.1.

Listing 7.1. STRUCT1.C.

/* STRUCT1, written 1992 by Peter D. Hipson * This is a simple structure program.

*/

#include <stdio.h> // Make includes first part of file #include <string.h> // For string functions

int main(void); // Define main() and the fact that this

// program doesn’t use any passed parameters

int main()

{

continues

193

Part II • Managing Data in C

Listing 7.1. continued

int

i;

 

struct

 

 

{

 

 

 

char

szSaying[129];

 

int

nLength;

} MySaying =

{“Firestone’s Law of Forecasting:”, strlen(MySaying.szSaying)};

printf(“sizeof(MYSaying) = %d\n”, sizeof(MySaying));

printf(“MySaying %p %3d ‘%s’\n”, &MySaying.szSaying, MySaying.nLength, MySaying.szSaying);

printf(“\n\n”);

return (0);

}

In STRUCT1, you can see the definition of the MySaying structure. This structure has two members: a character string (with a length of 129) called szSaying and an integer variable called nLength. The structure is initialized with a line of text and a number. The program then initializes the nLength member to the length of the string in the szSaying member. (Using a function call to initialize a data object is permitted but uncommon.)

Notice how the program refers to each member in the structure. The shorthand for a structure reference is the structure name followed by a period and the member name:

structure.member

If the member is also a structure (more on this later), the member name is followed by a period and its member name:

structure.memberstructure.member

194

C Structures

C C C

 

C7C

 

C C C

 

C

Arrays of Structures

As mentioned, an array can consist of any data type. In this section, you look at an example of a program that uses an array of type struct. Listing 7.2, STRUCT2, creates a structure, makes it an array, and initializes it.

! ! ! ! ! ! ! !

! ! ! ! !

! ! ! ! ! ! ! !

! ! ! ! !

! ! ! ! ! ! ! !

! ! !

! !

! ! ! ! ! ! ! ! ! ! ! ! !

! ! ! ! ! ! ! ! ! ! ! ! !

! ! ! ! ! ! ! ! ! ! ! ! !

! ! ! ! ! ! ! ! ! ! ! ! !

Some compilers will not compile Listing 7.2 correctly, even though it is legitimate ANSI code.

Listing 7.2. STRUCT2.C.

/*

STRUCT2, written 1992 by Peter D. Hipson

*

This program creates an array of type struct

*/

 

#include <stdio.h> // Make includes first part of file #include <string.h> // For string functions

int main(void); // Define main() and the fact that this

// program doesn’t use any passed parameters

int main()

{

int i;

struct

{

char

szSaying[129];

int

nLength;

} MySaying[] = {

“Firestone’s Law of Forecasting:”, 0,

continues

195