C++ Tutorials



Tutorials > C++ > Structures

View Full Source

What are Structures?

Structures allow you to specify a data type that consists of a number of pieces of data. This is useful to describe a single entity that is represented by a more than one variable.

How are structures defined?

Structures are defined using the struct keyword. They can be defined in two ways. One way is to specify a structure as follows :

    struct struct name { data types and statements };

This structure can now be used to create a number of variables in your program. Another way of defining a structure is as follows :

    struct { data types and statements } struct_name;

This way of defining a structure creates only one variable named struct_name. You cannot create more than one variable using this structure.

Contents of main.cpp :

#include <iostream>
#include <stdlib.h>

using namespace std;

Below we create a structure to hold information on a Player. Such items include the name, health, mana and strength of the player. A flag is also used to determine if he/she is alive. The way this structure has been created allows for multiple Player variables to be created.

struct Player
	char *name;
	int health;
	int mana;
	int strength;
	bool dead;

Below we create a World structure. As there is only one world, we create the struct with the name after the contents of the structure. This creates only one variable with the name World.

	int numPlayers;
	int maxPlayers;
} World;

int main()

A Player variable can now be created like any other normal variable.

	Player player1;

Variables that are part of the structure can be accessed using the dot(.) operator. A . must be placed after the variable which must then be followed by the variable that you are wanting to access. The variables can therefore be initialized as shown below. The values of these variables can be retrieved in the same way. = "Fred"; = 100;
	player1.dead = false;

	cout << << " : "
		<< << endl;

Another player is created below. Another way of initializing a structure is shown below. After declaring the variable, an equals sign can be placed follwed by curly brackets( {} ). The value of each variable can then be initialized by placing their values in the order as shown in the structure definition separated by commas.

	Player player2 = {

	cout << << " : "
		<< << endl;

The code below shows how the World can be accessed straight away. This is the only variable of this type.

	World.numPlayers = 2;
	World.maxPlayers = 5;

	cout << World.numPlayers <<
		" of " << World.maxPlayers
		<< endl;


	return 0;

You should now be able to create simple structs. More advanced features of structs will appear in the next tutorial.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 26 - Typedef Tutorial 28 - Advanced Structures >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer