Skip to main content

Structure In C++ Programming

Definition

Structure is collection of heterogeneous (different) data types. It is declared by struct keyword. All members of structure by default are public and can be access outside structure by reference variable (structure variable).  

Structure elements stored at contiguous memory location. Structure is similar to class except it does not have access specifier (Public, Private, Protected).

For example if we want to collect information of a student (Name, Roll Number, Class etc) than we can group these different type of information into a single unit by structure.

Syntax

struct <structure name>
{
    element 1;
    element 2;
    ------------
    ------------
};


Declaration of Structure

Structure is declared by struct keyword followed by an identifier i.e. the structure name.for example:
struct Student

Structure body starts with an opening curly braces and ends with closing curly braces. Closing curly braces is followed by a semicolon to end the declaration of structure. for example:
struct Student
{

};

Inside the curly braces of structure we can declare variable along with their data types. for example:
struct Student
{
   char Name[25];
   int Roll_No;
   float Marks;
};

here Student is a structure and Name, Roll_No and Marks are the member variable of structure.

like class, structure is a blueprint for reference variable (structure variable). 

When a structure declared it does not occupy space in memory but when we declare it's reference variable (structure variable) that occupy space in memory equal to the sum of size of variables memory present inside structure.

structure is a concept but reference variable (structure variable) is the real representation of structure.

Reference Variable (Structure Variable) Declaration

To declare a reference variable first we will write structure name and after that we will write reference variable name followed by a semicolon. for example:

struct Student stud;

Here Student is the structure name and stud is the reference variable. For reference variable structure works as data type. 

In above example stud is a reference variable and  Student is the data type for this reference variable and this reference variable can use only those values which are present inside Student structure.

Using Member (Member Variables) of Structure

Member variable of a structure can be access by reference variable using dot (.) operator. For example:

stud .Name="Amir Beg";
stud .Roll_No =1002;
stud .Marks = 89.50f;

Comments

Popular posts from this blog

Array and Two-D Array In Java Programming

Introduction An Array is a collection of constants of similar data type which are reference by a common name i.e. the array name. Array elements store at contiguous (together in sequence) memory location. The lowest address corresponds to the first element and the highest address to the last element. Array can be of any data type but it always stores elements of its own type, i.e. array of integer type will store integers only not any other type values. Need of Array We need array to store multiple values of similar type for which multiple declaration of variable is not possible. Like if we want to store marks of 50 students then it is not possible to declare 50 variables for them but we will simply use an array of integer to store these marks. Indexing in Array Array index always starts from 0. Array first element stores at 0th index and last element’s position will be one less than the total size of array. Subscript Subscript is the index of array....

Control Structure in Java Programming

Control Structure Control structure or control statements are the statements which control the flow of program execution. Control Structures are just a way to specify flow of control in programs. Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. These are of four types. 1. Conditional or Decision making statement. Example : if, if-else, nested if-else 2. Selection statement and branching. Example : else if ladder, switch, nested switch 3. Iteration or looping. Example for, while, do-while, for each loop 4. Jump statement. Example: break, continue Decision Making and Branching Decision Making or Conditional Statements In programming we face some situations where we want certain block of code to be executed when some condition is fulfilled. Decision maki...

Pointer In C++ Programming

Definition A pointer is a variable that holds a memory address, usually the location of another variable in memory. Pointer variable hold address of it’s own type variable i.e. an integer pointer will hold address of integer type variable and a character pointer will hold address of character type variable. Use of pointer 1.      Pointers provide the means through which the location of a variable can be directly accessed  and hence can be manipulated in the way as required. 2.       Pointers support c++’s dynamic allocation routines. Disadvantages 1.       Un-initialized, or wild pointers can cause system to crash. 2.       It is easy to use pointers incorrectly, causing bugs that are very difficult to find. Syntax for pointer declaration       Data_type       * variable_name; // * can be on right side     ...