codescracker


c++

C++ Pointers and Structures



« Previous Tutorial Next Tutorial »


C++ allows pointers to structures just as it allows pointers to int or char or float or any other data type. The pointers to structures are known as structure pointers.

Declaration and Use of Structure Pointers in C++

Just like other pointers, the structure pointers are declared by placing asterisk (∗) in front of a structure pointer's name.

It takes the following general form :

struct-name ∗struct-pointer ;

where struct-name is the name of an already defined structure and struct-pointer is the pointer to this structure. For example, to declare dt-ptr as a pointer to already defined structure date, we shall write

date ∗ dt-ptr ;

The declaration of structure type and the structure pointer can be combined in one statement. For example,

struct date
{
	short int dd, mm, yy ;
} ;
date ∗ dt_ptr ;

is same as

struct date
{
	short int dd, mm, yy ;
} ∗ dt_ptr ;

Using structure pointers, the members of structures are accessed using arrow operator ->. To refer to the structure members using -> operator, it is written as

struct-pointer -> structure-member

That is, to access dd and yy with dt_ptr, we shall write

dt_ptr -> yy

and

dt_ptr -> dd

C++ Structure Pointers Example

Let's look at the following program which demonstrates the usage of these pointers :

/* C++ Pointers and Structures. This C++ program
 * demonstrates about declaration and the use of
 * Structure Pointers in C++ */

#include<iostream.h>
#include<conio.h>
void main()
{
	struct date
	{
		short int dd, mm, yy;

	}join_date = {19, 12, 2006};

	date *date_ptr;
	date_ptr = &join_date;

	cout<<"Printing the structure elements using the structure variable\n";
	cout<<"dd = "<<join_date.dd<<", mm = "<<join_date.mm<<", yy = "<<join_date.yy<<"\n";
	cout<<"\nPrinting the structure elements using the structure pointer\n";
	cout<<"dd = "<<date_ptr->dd<<", mm = "<<date_ptr->mm<<", yy = "<<date_ptr->yy<<"\n";

	getch();
}

When the above C++ program is compile and executed, it will produce the following output:

c++ pointers and structures

The above program is self-explanatory. Remember that the dot operator ('.') requires a structure variable on its left and the arrow operator ('->') requires a structure pointer on its left. The arrow operator consists of the minus sign followed by greater than sign.

There are the following two primary uses of for structure pointers :

The structure pointer are very much useful in generating call by reference call to function because when a pointer to a structure is passed to a function, only the address of the structure is passed. This makes way for very fast function calls. A second advantage of it is when a function needs to reference the actual structure argument instead of a copy of them, by passing a pointer, it can modify the contents of the actual elements of the structure used in the call.

Here is another example program given, that you can take a look, for the complete understanding on the structure pointers in C++

/* C++ Pointers and Structures. This C++ program
 * also demonstrates the structure pointer in C++ */

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct emp
{
	int empno;
	char empname[20];
	float empbasic;
	float empexperience;
};
void display(emp *e);
void increase(emp *e);
void main()
{
	clrscr();
	emp mgr, *eptr;
	cout<<"Enter employee number: ";
	cin>>mgr.empno;
	cout<<"Enter name: ";
	gets(mgr.empname);
	cout<<"Enter basic pay: ";
	cin>>mgr.empbasic;
	cout<<"Enter experience (in years): ";
	cin>>mgr.empexperience;
	eptr = &mgr;
	cout<<"\nEmployee details before increase()\n";
	display(eptr);
	increase(eptr);
	cout<<"\nEmployee details after increase()\n";
	display(eptr);
	getch();
}
void display(emp *e)
{
	int len=strlen(e->empname);
	cout<<"Employee number: "<<e->empno;
	cout<<"\nName: ";
	cout.write(e->empname, len);
	cout<<"\tBasic: "<<e->empbasic;
	cout<<"\tExperience: "<<e->empexperience<<" years\n";
}
void increase(emp *e)
{
	if(e->empexperience >= 5)
	{
		e->empbasic = e->empbasic + 15000;
	}
}

Here are the two sample runs of the above C++ program. One for the employee having experience less than 5 years and the other having experience more than 5 years.

c++ structure pointers

c++ structure pointers example program

Self Referencial Structures in C++

A structure having a member element that refer to the structure itself, is known as Self-Referencial Structure.

To understand the self-referencial structures, we shall first talk about a situation. Suppose you are required to write a program that stores and manipulates details of quite-many employees, details like empno, name, basic, and experience etc. Going through this problem, this immediately suggests the use of structures to hold the information. However, how will these structures be stored and manipulated ? For this problem, you may use a fixed-size array of structures. But a fixed-size array has some serious drawbacks. An important one of them is, the size of the array arbitrarily limits the length of the list of employees. What if you realize during program-run that the array should have been of bigger size ? To avoid such a problem, you would certainly go for a dynamic list so that the list can be as large as free memory allows.

A dynamic linear list in C++ is called a linked-list. One major use of linked lists is to create arrays of unknown size in memory.

A linked list requires that each item of information contain a link to the next element in the list. Each data item usually consists of a structure that includes information fields and a link pointer. You will learn about linked list in the later chapter in detail.

A linked list is a structure having an additional pointer as its element that points to a structure of the same type.

That is to declare the above shown structure type, we shall write

struct Employee
{
	int empno ;
	char name[26] ;
	float basic ;
	float experience ;
	Employee ∗ next ;       / pointer to same structure type
};

See the structure contains an element that refers to the structure itself. Such a structure is called self-referencial structure.

C++ Dynamic Structures

The new operator can be used to create dynamic structures also i.e., the structures for which the memory is dynamically allocated. To do so, we write in the following form :

struct-pointer = new struct-type ;

where struct-type is the predefined structure type for which the memory is being allocated and struct-pointer is a pointer of struct-type type that stores the address of newly allocated memory. For instance, to allocate memory dynamically for Employee structure defined above, we may give

Employee ∗eptr ;
eptr = new Employee ;

A dynamic structure can be released using the deallocation operator delete as shown below :

delete struct-pointer ;

where struct-pointer is the same pointer that was pointing to the dynamically allocated memory. That is, to release memory which is being pointed to by eptr, we shall write

delete eptr ;

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test