NIOS Computer Science: Chapter 12 – Structure, Typedef & Enumerated Data Type Part 1

Download PDF of This Page (Size: 328K)

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;

}

1. 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).

2. Accessing of Data Members

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

structure variable. member name

3. Initialization of Structure Variable

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

Student S2 = {“ANKITA”, 32};

Developed by: