Subscribe

Enter your email address:

Categories

 

February 2012
M T W T F S S
« Dec    
 12345
6789101112
13141516171819
20212223242526
272829  

Archives

Disclaimer

© 2009 Zero Intellect. All rights reserved. The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway. This material is not sponsored or endorsed by any of the vendors mentioned in this website and their Logos are trademarks of their own and their affiliates.

Overview of a structure in C and C++

 

The following article gives a brief overview of the structure in C/C++. The structure was first introduced in C where the need to group variables into a single record arose. Variables normally were assigned a single value or were assigned multiple values of the same type (array). The introduction of the structure allowed for variables to contain records of multiple values of different types. A simple comparison is displayed in the figure below

A structure is of the format below and consists of 3 major components

1. struct-type-name
2. structure-variable
3. record data fields (data type, variable-name)

 

struct [<struct-type-name>]
{
 [<type> <variable-names>];
 [<type> <variable-names>];
 [<type> <variable-names>];
 …
}[<structure-variables>];

The <struct-type-name> is an optional tag name that refers to the structure type. It should be defined to give a reflection of the contents of the structure. Such as ‘student’ to indicate a record of student details. The <structure-variables> are the data definitions, and are also optional.

Though both are optional, one of the two must appear (makes logical sense, otherwise we would not be able to retrieve or store any values in the records). Elements in the record are defined by naming a <type>, followed by <variable-names> separated by commas. Different variable types can be separated by a semicolon.

A sample program follows that has a single record structure-variable as well as a structure-variable that holds 2 records

 

# include <iostream.h>
# include <conio.h>

void main()
{
 clrscr();

 struct contact
 {
  char name[20];
  int age;
 }mrx;

 cout<<”Name: “;
 cin>>mrx.name;
 cout<<”Age: “;
 cin>>mrx.age;
 cout<<”\nmrx.name : “<<mrx.name<<”\nmrx.age : “<<mrx.age;

 struct contact family[2];

 cout<<”\n\nEnter details of Family Members…\n”;

 for(int i=0;i<2;i++)
 {
  cout<<”\nName: “;
  cin>>family[i].name;
  cout<<”Age: “;
  cin>>family[i].age;
 }

 cout<<”\nDetails of Family Members…”;

 for(i=0;i<2;i++)
 {
  cout<<”\n\nName :\t”<<family[i].name<<”\nAge :\t”<<family[i].age;
 } 
      
 getch();
}

 

To access elements in a structure, you use a record selector (.)

For example, as it is mentioned in the program above mrx.age

To declare additional variables of the same type, you use the keyword struct followed by the <struct-type-name>, followed by the variable names

For example, the following declaration will create a record of 5 rows containing colleague details (age, name) as per our example program above

struct colleagues[5];

To summarize, structures were created to hold records of different data type values. But in C++ which is an object oriented language, classes were introduced that have many more benefits than structures. However, for simple implementations structures are very useful.

1 comment to Overview of a structure in C and C++