ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾C/C++±à³Ì£¾Tutorial of C++

Lesson 7: Structures

Before discussing classes, this lesson will be an introduction to data structures similar to classes. Structures are a way of storing many different variables of different types under the same name.

The format for declaring a structure(in C++) is

struct NAME
{
  VARIABLES;
};

Where NAME is the name of the entire type of structure. To actually create a single structure the syntax is NAME name_of_single_structure; To access a variable of the structure it goes name_of_single_structure.name_of_variable;

For example,

struct example
{
  int x;
};

example an_example;
an_example.x=33;

Here is an example program

struct database
{
  int id_number;
  int age;
  float salary;
};

int main()
{
  database employee;  //There is now an employee variable that has modifiable 
                //variables inside it.
  employee.age=22;
  employee.id_number=1;
  employee.salary=12000.21;
  return 0;
}

The struct database declares that database has three variables in it, age, id_number, and salary.

You can use database like a variable type like int. You can create an employee with the database type as I did above. Then, to modify it you call everything with the 'employee.' in front of it. You can also return structures from functions by defining their return type as a structure type. Example:

struct database fn();

I suppose I should explain unions a little bit. They are like structures except that all the variables share the same memory. When a union is declared the compiler allocates enough memory for the largest data-type in the union. Its like a giant storage chest where you can store one large item, or a bunch of small items, but never the both at the same time.

The '.' operator is used to access different variables inside a union also.

As a final note, if you wish to have a pointer to a structure, to actually access the information stored inside the structure that is pointed to, you use the -> operator in place of the . operator.

A quick example:

#include <iostream.h>

struct xampl
{
  int x;
};

int main()
{  
  xampl structure;
  xampl *ptr;
  ptr=&structure; //Yes, you need the & when dealing with structures
         //and using pointers to them
  cout<<ptr->x;  //The -> acts somewhat like the * when used with pointers
            //It says, get whatever is at that memory address
            //Not "get what that memory address is"
  return 0;
}