15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

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

536<br />

Data Structures<br />

Creating a structure<br />

A structure is a variable that typically holds two or more variables. To create<br />

a structure, use the struct keyword as follows:<br />

struct name {<br />

datatype variable;<br />

};<br />

The name of a structure can be any descriptive name, such as baseball_<br />

team or sales_dept. Inside a structure, you must declare one or more<br />

variables. A typical structure might look like this:<br />

struct MyGrades {<br />

char grade;<br />

int class_number;<br />

};<br />

After defining a structure, you can declare a variable to represent that structure,<br />

such as<br />

struct MyGrades chemistry;<br />

As a shortcut, you can define a structure and declare a variable to represent<br />

that structure as follows:<br />

struct MyGrades {<br />

char grade;<br />

int class_number;<br />

} chemistry;<br />

Creating enumerated variables<br />

Enumerated variables act like a list that lets you name a group of items, such<br />

as the days of the week, names of people in a group, and so on. A typical<br />

enumerated variable list might look like this:<br />

enum name {item1, item2, item3};<br />

So if you wanted to define the names of the days in a work week, you could<br />

do the following:<br />

enum weekend {saturday, sunday};

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

Saved successfully!

Ooh no, something went wrong!