C++ structures, typedef and unions

Structures are used to group together different data elements (types of variables) under the same name. These data elements, known as members, can have different types and different lengths.

Take look at the syntax of a structure:


	struct structure_name {
		type member_name1;
		type member_name2;
	} object_names;

For example you could create a structure “telephone”: which is made up of a char* (that is used to hold the name of the person) and an integer (that is used to hold the telephone number).

Take a look at the example:


	struct telephone
	{
		char *name;
		int number;
	};

Note: the ; behind the last curly bracket.

With the declaration of the structure you have created a new type, called telephone. Before you can use the type telephone you have to create a variable of the type telephone. Take a look at the following example:


	#include<iostream>
	using namespace std;

	struct telephone
	{
		char *name;
		int number;
	};

	int main()
	{
		struct telephone index;

		return 0;
	}

Note: index is now a variable of the type telephone.

To access the members of the structure telephone, you must use a dot between the structure name and the variable name(variables:name or number.) Take a look at the next example:


	#include<iostream>
	using namespace std;

	struct telephone
	{
		char *name;
		int number;
	};

	int main()
	{
		struct telephone index;

		index.name = "Jane Monroe";
		index.number = 12345;

		cout << "Name: " << index.name << '\n';
		cout << "Telephone number: " << index.number;

		return 0;
	}

Type definitions and structures

Type definitions make it possible to create your own variable types. In the following example we will create a type definition called “intpointer” (a pointer to an integer):


	#include<iostream>
	using namespace std;

	typedef int *int_ptr;

	int main()
	{
		int_ptr myvar;
		return 0;
	}

It is also possible to use type definitions with structures. The name of the type definition of a structure is usually in uppercase letters. Take a look at the example:


	#include<iostream>
	using namespace std;

	typedef struct telephone
	{
		char *name;
		int number;
	}TELEPHONE;

	int main()
	{
		TELEPHONE index;

		index.name = "Jane Monroe";
		index.number = 12345;

		cout << "Name: " << index.name << '\n';
		cout << "Telephone number: " << index.number;

		return 0;
	}

Note: The word struct is not needed before TELEPHONE index;

Pointer to structures

If you want a pointer to a structure you have to use the -> (infix operator) instead of a dot. Take a look at the following example:


	#include<iostream>
	using namespace std;

	typedef struct telephone
	{
		char *name;
		int number;
	}TELEPHONE;

	int main()
	{
		TELEPHONE index;
		TELEPHONE *ptr_myindex;

		ptr_myindex = &index;

		ptr_myindex->name = "Jane Monroe";
		ptr_myindex->number = 12345;

		cout << "Name: " << ptr_myindex->name << '\n';
		cout << "Telephone number: " << ptr_myindex->number;

		return 0;
	}

Note: The -> (infix operator) is also used in the cout statement.

Unions

A union is like a structure in which all members are stored at the same address. Members of a union can only be accessed one at a time. The union data type was invented to prevent memory fragmentation. The union data type prevents fragmentation by creating a standard size for certain data. Just like with structures, the members of unions can be accessed with the . and -> operators. Take a look at the example:


	#include<iostream>
	using namespace std;

	typedef union myunion
	{
		double PI;
		int B;
	}MYUNION;

	int main()
	{
		MYUNION numbers;
		numbers.PI = 3.14;
		numbers.B = 50;

		return 0;
	}

That’s all for this tutorial. In the next tutorial we take a look classes.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C++ structures, typedef and unions”

Why not let us know what you think by adding your own comment!

  1. Matt on November 10th, 2009:

    There is no longer any need to declare a variable as ‘struct var;’, the ‘struct’ should be dropped. Also, since this is true, there’s no need for the tip on typedeffing the struct. Both of these were tricks used in C, but have no use in C++

    An example:

    #include

    struct A_Struct{
    int some_var;
    };

    int main(){
    A_Struct str;
    str.some_var = 255;
    std::cout << str.some_var << std::endl;
    return 0;
    }

  2. messi on November 20th, 2012:

    Hi, really great tutorial..

    why have you decleared name as a pointer type? I am basically trying to figure out the propre use of poiners by reading code. Here, i do not understand how a name pointer could be usefull..