S T R U C T U R E S

PART 1: What the hell is this struct?
A structure is simply a way of grouping several variables under one name. When you declare a structure type you use the struct keyword, followed by what you want to call this, then an opening curly brace, then the variables you want grouped under this structure type, and lastly a closing curly brace with a semi-colon. All this sounds so darn complicated. But I'll give an example. We create a structure type that contains three pieces (three variables), year, month, and day:
struct date
{
  int year;
  int month;
  int day;
};
My simple analogy of this would be how you store files on your computer. Most of us eventually start making folders to group similar things in different places. For example you may have a folder called "cpp" in which you store all your C++ source files:

... so using this analogy, struct date would look like ...

Now actually when we're using the struct keyword in the above example we're not creating any variables. What we're doing is constructing a blue print type dealy FOR making variables. To actually create variables using this "blue print" we simply put the name of the struct you made, followed by the variable you want to create. So it follows this form:

[struct type] [variable];

So we could create variables using our blue print, by doing something like the following:
date dt;
date md;
date dob;
PART 2: How Do I Use It?
Here's where I explain the way you take and use the seperate pieces of one structure. Below is a small sample program that does not use a structure. It declares three integer variables that make up a date (year, month, and day) and then gets user input to fill these variables with values.
/* -example 1--------------------------------------------------------------- */

#include <iostream.h>

void main()
{
  int year, month, day;

  cout << "Please input the following: ";
  cout << "Year   :";
  cin >> year;
  cout << "Month  :";
  cin >> month;
  cout << "Day    :";
  cin >> day;
}

/* ------------------------------------------------------------------------- */
Now the following is an example program that does the same thing except it uses a structure. Take a look, and explanation follows.

/* -example-2--------------------------------------------------------------- */

#include <iostream.h>

struct date
{
  int year;
  int month;
  int day;
};

void main()
{
  date dt;

  cout << "Please input the following: ";
  cout << "Year   :";
  cin >> dt.year;
  cout << "Month  :";
  cin >> dt.month;
  cout << "Day    :";
  cin >> dt.day;
}

/* ------------------------------------------------------------------------- */

We declared the structure date to contain three pieces, which of course are year, month, and day. Now then in main() we create a variable called dt using our date structure. In essence this creates those three pieces under that one name. So dt has three pieces that call it momma:

dt
|-- year
|-- month
\-- day

Now we can use these pieces almost just like normal variables. Because that's all they are! They're normal variables just "grouped" under the one object dt. To access variables within a structure you put the name of the struct followed by a period (.) and then lastly the name of the variable within that you wish to access:

[struct].[variable]

So say we want to use the year part of dt: dt.year. Or month: dt.month.

How does this become useful to us? Well, what if you want to have the date of birth for three people. You could do it without structs, most definetly. The following is an example of that:

/* -example-3--------------------------------------------------------------- */

#include <iostream.h>

void main()
{
  int neil_dob_year, neil_dob_month, neil_dob_day;
  int missy_dob_year, missy_dob_month, missy_dob_day;
  int jae_dob_year, jae_dob_month, jae_dob_day;

  neil_dob_year = 1979;
  neil_dob_month= 8;
  neil_dob_day  = 19;

  missy_dob_year = 1977;
  missy_dob_month= 10;
  missy_dob_day  = 30;
}

/* ------------------------------------------------------------------------- */

But this is so unelegant. It requires us to declare many variables even though each person's date of birth contain common things. So below is the same thing except using structs.

/* -example-4--------------------------------------------------------------- */

#include <iostream.h>

struct date;
{
  int year, month, day;
}

void main()
{
  date neil_dob;
  date missy_dob;
  date jae_dob;

  neil_dob.year = 1979;
  neil_dob.month= 8;
  neil_dob.day  = 19;

  missy_dob.year = 1977;
  missy_dob.month= 10;
  missy_dob.day  = 30;
}

/* ------------------------------------------------------------------------- */
PART 3: Functions and Structs, A Marriage Made in Hell?
Actually no ... structs and functions are a good combination. Though they may seem difficult, it's actually quite simple and very convenient. Say for example we want to make a function that tells you the date when you pass three parameters: y (year), m (month), and d (day). Well, below is a simple example of this.

/* -example-5--------------------------------------------------------------- */

#include <iostream.h>

void print_date(int y, int m, int d);

void main()
{
  int year, month, day;

  year = 1979;
  month= 8;
  day  = 19;

  print_date(year,month,day);
}

void print_date(int y, int m, int d)
{
  cout << "The date is: " << m << '/' << d << '/' << y;
}

/* ------------------------------------------------------------------------- */

In main() we create three integers (year, month, and day). We pass them to print_date() where y becomes what we passed as year, m what we passed as month, etc. And then in the function I simply say, "The date is: month/day/year". This requires that we pass three parameters which can become a hassle. Sometimes its just easier to pass one. So below is the version using a structure.

/* -example-6--------------------------------------------------------------- */

#include <iostream.h>

struct date
{
  int year, month, day;
};

void print_date(date d);

void main()
{
  date dt;

  dt.year = 1979;
  dt.month= 8;
  dt.day  = 19;

  print_date(dt);
}

void print_date(date d)
{
  cout << "The date is: " << d.month << '/' << d.day << '/' << d.year;
}

/* ------------------------------------------------------------------------- */

The End (for now, *muh ha ha ha*)

Page Content & Design ©1998
By Neil C. Obremski

home

prfce

loop

refs

ptrs

struct

class

array

rsrc

site

indx