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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

NOTE<br />

Wad<strong>in</strong>g In Deeper<br />

You can create <strong>in</strong>stances of a structure when you declare the structure.<br />

At the end of the structure declaration, <strong>in</strong>sert a variable name (one or<br />

more) between the clos<strong>in</strong>g brace and the semicolon that follows the<br />

structure declaration. Here’s an example:<br />

struct po<strong>in</strong>t {<br />

<strong>in</strong>t x;<br />

<strong>in</strong>t y;<br />

} upperLeft, lowerRight;<br />

This code declares the structure and creates two <strong>in</strong>stances of the<br />

structure with variable names upperLeft and lowerRight.<br />

Now that I have the structure declared, I need to put it to use. I first need to create an <strong>in</strong>stance<br />

of the structure. Here’s how that looks:<br />

mail<strong>in</strong>gListRecord record;<br />

This statement allocates memory for the structure (120 bytes, give or take) and assigns that<br />

memory to a variable named record. Now that I have an <strong>in</strong>stance of the structure set up, I<br />

can assign values to the data members:<br />

strcpy(record.firstName, “Bruce”);<br />

strcpy(record.lastName, “Reisdorph”);<br />

strcpy(record.address, “123 Inspiration Pt.”);<br />

strcpy(record.city, “Merced”);<br />

strcpy(record.state, “CA”);<br />

record.zip = 95031;<br />

record.aFriend = true;<br />

record.aFoe = false;<br />

There is someth<strong>in</strong>g you haven’t seen yet <strong>in</strong> this code. In order to access the data members of<br />

a structure, you need to employ the structure member operator, which is a period placed<br />

between the variable name and the data member. (If you forget to add the structure member<br />

operator, you will probably have the compiler wh<strong>in</strong><strong>in</strong>g about undef<strong>in</strong>ed symbols.) The<br />

structure member operator allows you to access a particular member of the structure—either<br />

to read the value of the data member or to change the value of the data member.<br />

If you want to, you can <strong>in</strong>stantiate an object and supply its members all at one time:<br />

mail<strong>in</strong>gListRecord rec = {<br />

“Bruce”,<br />

“Reisdorph”,<br />

“123 Inspiration Pt.”,<br />

“Merced”,<br />

“CA”,<br />

95031,<br />

true,<br />

false<br />

};<br />

57<br />

2

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

Saved successfully!

Ooh no, something went wrong!