Home » Learn C++ » C++ Structures

C++ Structures

C++ Structures:

A structure is a collection of related elements or data items. The elements of the structure are called members of the structure. A structure is unlike an array. All elements in an array are of the same type. But in structures, the elements can be of different types.
The syntax of a structure is very similar to that of a class in C++.  The structures are commonly used in file processing. A file is a collection of related records and a record is a collection of related fields of same or different data type. The structures are also a collection of same or different data types. These are, therefore, also known as records.

Defining a Structure:

Specifying the names, types and number of data items within a structure is called defining of the structure. The keyword “struct” is used to define a structure.
The syntax for defining a structure is:
struct st_name
{
type 1;

type m;
type n;

Where
st name represents the structure name. It is also ca1led structure tag. It is used to declare variables 6f structure type.

type represents the data type of a variable.

l, m, n represents members of the structure. These may have different or same data type. The members are enclosed in braces. A semi-colon after the closing brackets ( }; ) indicates the end of the structure. Each member of a structure must have unique name but different structures may have same names.

A structure is first defined and then variables of structure type are declared. A variable of a structure type is declared to access data in the members of the structure.

Example: Declare a structure with address as tag and having two members name of character type and age of integer type.
struct address
{
char name [15];
int age ;

The structure member name is of character type with 15 characters length (including the null character) and age is of integer type.

Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.