Neil's C++ Stuff



C++ Structure Variables Tutorial

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:

Struct Analogy: Disk with Folders « in this analogy struct date resembles » Struct Analogy: Date

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;

How Do I Use struct in C++?

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 mama:

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 bob_dob_year, bob_dob_month, bob_dob_day;
  int mary_dob_year, mary_dob_month, mary_dob_day;
  int fido_dob_year, fido_dob_month, fido_dob_day;

  bob_dob_year = 1979;
  bob_dob_month= 8;
  bob_dob_day  = 19;

  mary_dob_year = 1977;
  mary_dob_month= 10;
  mary_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 bob_dob;
  date mary_dob;
  date fido_dob;

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

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

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

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;
}

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


Contact

Comments or questions? Email me at webmaster@neilstuff.com.