Structure: Introduction and Objectives, Variable of the Structure, Accessing of Data Members and Initialization of Structure Variable

Get unlimited access to the best preparation resource for UGC : Get complete video lectures from top expert with unlimited validity: cover entire syllabus, expected topics, in full detail- anytime and anywhere & ask your doubts to top experts.

Download PDF of This Page (Size: 244K)

Image of typedef with structure

Image of Typedef with Structure

Image of typedef with structure

Introduction

  • Sometimes it is required to store information, which can be of same or different types.

  • For this purpose, structure statement is used.

  • This lesson contains the three important statements of C++ language.

Objectives

After going through this lesson, you would be able to:

  • define variable, initialize and access members of the structure

  • explain the concept of nested structure

  • explain typedef statement in a program

  • define Enum statement and use it

Structure

Image of Structure

Image of Structure

Image of Structure

  • A structure is a collection of simple variables which can be of same or different types.

  • The data items in a structure are called the members of the structure. Consider the following example:

# include < iostream.h >

struct student

{

char name [20].

int marks.

};

void main ()

{

student S1, S3.

cin >> S1. name >> S1. marks.

cout << S1. name << S1. marks.

student S2 = {“ANKIT”, 32}.

cout << S2. name << S2. marks.

S3 = S2.

cout << S3. name << S3. marks.

}

Variable of the Structure

S1 and S2 are called variable of the structure student.

They are given enough space in memory to hold the members of the structure (22 bytes each for above program).

Accessing of Data Members

The accessing of data members is done by using the following format:

structure variable. member name

Initialization of Structure Variable

Initialization is done at the time of declaration of a variable. For example:

Student S2 = {“ANKITA”, 32}.

Developed by: