Back to home

Topics

1.Define Structure?
A structure is a collection of variables referenced under one name. Structure is a user defined data type.

Syntax:

    struct structure_name

     {

          structure element 1;

          structure element 2;

          structure element 3;

          ..........

          ..........

     };
Where struct is the keyword. structure_name is the structure tag. structure element 1, structure element 2 and structure element 3... are called member vaiables. Semicolon must be put behind the closing curly brace.
2.Explain Structure Declaration?
Structures are declared with the struct keyword, which informs the compiler that a structure is being defined. The following statements declare the structure type:

struct student

{

      int rollno;

      char name;

      int age;

      char class;

      int mark;

      char grade;

 };

This statement defines a new data type named student, which consists of three character variables such as name, class, and grade along with three integer variables such as rollno, age, and mark.
3.Explain Structure Initialisation?
Structure variables can also be initialised where they are declared. The format used is quite similar to that used to initialise arrays.

struct student

{

       int rollno;

       char name;

       int age;

       char class;

       int mark;

       char grade;

};

struct student std1 = {1,"Franklin", 17, "XII", 23, "A"};

struct student std2 ={2, "Anderson", 18, "XII", 12, "C"};

struct student std3 ={3, "Peter", 17,"XII", 24, "B"};
4.What is Accessing Structure Elements?
std3.age is used to access the age of the std3. Note that before the dot operator there must always be a structure variable and after the dot there must be a structure element.

Example:

    //structor.cpp

   #include<iostream.h>

   #include<conio.h>

   struct students

   {

         int id;

        char name[10];

        float mks;

  };

  void main( )

  {

        cirscr( );
       
        struct students s1,s2,s3;

        cout<<"Enter ID, Name, and Marks in each record:-\n";

       cout<<"Enter I record:";

       cin>>s1.id>>s1.name>>s1.mks;

       cout<<"Enter II record:";

       cin>>s2.id>>s2.name>>s2.mks;

       cout<<"Enter III record:";

       cin>>s3.id>>s3.name>>s3.mks;

       cout<<"\nIDNO\tName\tMarks\n";

       cout<<s1.id<<"\t"<<s1.name<<"\t"<<s1.mks<<endl;

       cout<<s2.id<<"\t"<<s2.name<<"\t"<<s2.mks<<endl;

       cout<<s3.id<<"\t"<<s3.name<<"\t"<<s3.mks<<endl;
 
  }

5.Explain Nested Structure?
Structure definitions that appear inside other structure definitions are nested structures.

Nested structures encapsulate structure definitions and make them an integral part of an enclosing structure definition.
NOTE: Pointer is a variable, which holds the address of another variable.

            Here, *bn holds the address of memory location where batchno variable is stored.
6.Explain accessing nested structure?
The .or-> operators are used to access the required member from a nested structure. With the help of the following statements let us understand how to access nested structure.

       batchno.subj1.mark

The statement accesses the value of mark variable declared in subj1 structure type.

     bn->subj2.mark

This statement uses-> with structure pointer bn and .with structure data member mark to access value of mark declared in subj2 structure type.
7.Describe arrays of structures?
The most common use of structures is in arrays of structures. To declare an array of structures, first define a structure, and then declare an array variable of that type.

For example, to declare a 100 element array of structures of type student, use

       struct student std_info[100];

This creates 100 sets of variables that are organised as declared in the structure type student.

To access a specific structure within the std_info array, index the array variable name.
For example, to print the age of the third structure:

      cout<<"%d"<<std_info[2].age;

Similar to all array variables, arrays of structures begin indexing at 0.
8.Explain the passing structure members to functions?
When a member of a structure is passed to a function, we are actually passing the value of that member to the function. Therefore, we are passing a simple variable (unless that element is compound, such as an array of characters).
9.Explain the passing entire structures to functions?
When a structure is used as an argument to a function, the entire structure is passed using the standard call-by-value method. This means that any changes made to the contents of the structure, inside the function to which it is passed, do not affect the structure used as an argument.
When using a structure as a parameter, the most important thing to remember is that the type of argument must match the type of parameter. The best way to do this is to define a structure globally and then use its tag name to declare structure variables and parameters as required.
10.Explain Reference argument/parameter?
An alternative parameter-passing mechanism that is available in C++ is called pass by reference. This mechanism makes it possible to pass a structure to a procedure and modify it.
11.What are the function of returning structures?
Returning structures is also the same as returning other data types. The return keyword, and a variable of the structure's type to pass the result are required to return the structure from a function. This can be done by specifying a structure as the return type of the function. This feature of C++ is useful where a structure has been passed by value to a function, the changes made to the contents of the structure are needed by the calling function without disturbing the original contents.

The following statements is the function prototype with return type structure student

      student func 1 (student s)      //s is the variable of type student

when the function func 1 is defined include:

      return s;

to return s to the main function.

Paid Users Only!
Paid Users Only!
Paid Users Only!
Cambridge Advanced Grade 12
IGCSE




Practice in Related Chapters
Object Oriented Programming in C++
Inheritance
Powered By