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

Download PDF of This Page (Size: 336K)

Image of Datatype

Image of Datatype

Image of Datatype

Diagram shows data types

Diagram Shows Data Types

Diagram shows data types

4. Structure Variable in Assignment Statements

The statement

S3 = S2;

assigns the value of each member of S2 to the corresponding member of S3.

Since a large structure can have many members, such an assignment statement requires the compiler to do a considerable amount of work.

Note that one structure variable can be assigned to another only when they are of the same structure type, otherwise compiler will give an error.

5. Structure Within Structure

This is called nesting of structure. Consider the following program in which date field is a structure.

# include < iostream.h >

# include < stdio.h >

struct today

{

int month;

int date;

int year;

};

struct biodata

{

char name [ 20 ];

today date_of_birth;

char add [ 30 ];

};

void main ( )

{

biodata b;

cout << “Enter name”;

gets (b.name);

cout << “Enter birth month”;

cin >> b.date_of _birth. month;

cout << “Enter birth date”;

cin >> b.date_of_birth.date;

cout << “Enter birth year”;

cin >> b.date_of _birth.year;

cout << “Enter address”;

gets (b.add);

cout << “Name” << b.name << “\n”;

cout << “Month” <<b.date_of_birth.month << “\n”;

cout << “Date” << b. date _of_birth. date << “\n”;

cout << “Year” << b. date_of_birth.year << “\n”;

cout << “Address” << b.add << “\n”;

}

6. Accessing Nested Structure Members

As the structure is nested inside another, we must apply the dot operator twice to access the structure members:

cin >> b. date_of_birth. month;

7. Initializing Nested Structure

It is initialized at the time of declaration of structure variable.

For example:

biodata bd = {“MOHIT”, {3, 21, 1991}, “Rohini”};

The inner braces are required just to separate the values, although it is ignored by the compiler.

Developed by: