top of page

Chapter 13-STRUCTURES

 

User defined  data types are created with th help of struct or class keywords .Astructure type is a user-defined composite type.It is composed of fields or membes that can have different types.In C++,a structue is the same as a class except that its membes are public by default.

Structures are declared with the truct keyword . The format is :

 

struct<struct tag>

{

member definition;

member definition;

              :

              :

              :

member definition;

}[one or more structure variables];

For example 

          struct person 

             {

                    char name[20];

                     int age; 

               }p,p1,p2;

 

 

 

STRUCTURE TAG 

A structure tag name can be used to declare variable of a structure type.

Structure elements::

The members of a structure are called structure elements .Structure elements are referenced through the dot'.' ir arrow '->' operators.

 

typedef.Every variable has a data type .typedef is used to define new data type  names to make a progam more readable to the programmer 

 

 

For example 

main()

{

int money

money=2;)

 

The main use for typedef is to define structures.For example :

typedef struct {int age ;char *name } person ;

person people ;

 

 

reference structure .

Function can return  structures or its reference.Structure can be passed to function either by passing its elements individually or by passing the entire structure .By default a structure variable is passed to a function by value but the array of structure s passed by reference.

 

 

 

 

 

 

 

 

 

Nested structure:

C++ enables you to nest one structure definition within another .This technique saves time when you are writing programs that use similar structures.

 

 

 

 

 

 

 

 

 

 

 

  • w-facebook
  • Twitter Clean
  • w-googleplus
bottom of page