Home | | Computer Science 11th std | C++: Declaring and defining structures

Chapter: 11th Computer Science : Chapter 12 : Arrays and Structures

C++: Declaring and defining structures

Structure is declared using the keyword ‘struct’.

Declaring and defining structures


Structure is declared using the keyword ‘struct’. The syntax of creating a structure is given below.

struct structure_name {

      type member_name1;

      type member_name2;

} reference_name;

Objects declared along with structure definition  are called global objects

An optional field reference_name can be used to declare objects of the structure type directly.

Example:

struct Student

{

      long rollno;

      int age;

      float weight;

} ;

In the above declaration of the struct, three variables rollno,age and weight are used. These variables(data element)within the structure are called members (or fields). In order to use the Student structure, a variable of type Student is declared and the memory allocation is shown in figure 12.5


struct Student balu; // create a Student structure for Balu

This defines a variable of type Student named as Balu. Similar to normal variables, struct variable allocates memory for that variable itself. It is possible to define multiple variables of the same struct type:

struct Student frank; // create a structure for Student Frank.

For example, the structure objects balu and frank can also be declared as the structure data type as:

struct Student

{

longrollno;

int age;

float weight;

}balu, frank;

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : C++: Declaring and defining structures |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.