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

Doorsteptutor material for competitive exams is prepared by world's top subject experts: get questions, notes, tests, video lectures and more- for all subjects of your exam.

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

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} .