12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

58 Day 2<br />

SYNTAX<br />

▼<br />

▲<br />

This saves you some typ<strong>in</strong>g over the first method I showed you, but is not always practical<br />

<strong>in</strong> real-world situations. In a real-world program a structure would likely be filled out as a<br />

result of user <strong>in</strong>put or possibly with data read from a file. Assign<strong>in</strong>g data to the structure like<br />

you see here is not practical <strong>in</strong> those situations.<br />

The struct statement:<br />

struct name {<br />

data_member_1;<br />

data_member_2;<br />

.<br />

.<br />

.<br />

data_member_n;<br />

} <strong>in</strong>stance;<br />

The struct statement declares a group<strong>in</strong>g of data members (data_member_1, data_member_2,<br />

…, data_member_n) and provides a name for this group<strong>in</strong>g (name). The optional <strong>in</strong>stance<br />

statement creates an occurrence of this group<strong>in</strong>g.<br />

Arrays of Structures<br />

Just as you can have arrays of <strong>in</strong>ts, chars, or longs, you can also have arrays of structures.<br />

Declar<strong>in</strong>g and us<strong>in</strong>g an array of structures is not terribly complicated:<br />

mail<strong>in</strong>gListRecord listArray[5];<br />

strcpy(listArray[0].firstName, “Chuck”);<br />

listArray[4].aFoe = true; // grrrrr!!<br />

// etc.<br />

This is only slightly more complicated than us<strong>in</strong>g an array of one of the <strong>in</strong>tegral data types.<br />

You will notice that the subscript operator and the structure member operator are used backto-back.<br />

Headers and Source Files<br />

The source file is an ASCII text file that conta<strong>in</strong>s the program’s source code. The compiler<br />

takes the source code file, parses it, and produces mach<strong>in</strong>e language that the computer can<br />

execute.<br />

One of the problems with books on programm<strong>in</strong>g is that they use simple examples to<br />

communicate concepts and ideas. You will undoubtedly f<strong>in</strong>d that th<strong>in</strong>gs are never that<br />

simple. So far, we have been deal<strong>in</strong>g with very short programs conta<strong>in</strong>ed <strong>in</strong> a s<strong>in</strong>gle source<br />

file. In the real world, a program of any consequence will have several source files. A program’s<br />

code is divided <strong>in</strong>to different source files for a number of reasons. One of the primary reasons<br />

is organization. By keep<strong>in</strong>g related chunks of code together, you can more easily f<strong>in</strong>d a certa<strong>in</strong><br />

section of code when needed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!