C++ Structure | Examples | Declare,initialize,define and size of structure | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Saturday, July 25, 2020

C++ Structure | Examples | Declare,initialize,define and size of structure | My CS Tutorial

Structure in c++ | Declaration, Initialization,Define a structure | Example of c++ structure | My CS Tutorial


C++ structure | My CS Tutorial


C++ Sructure(struct)


Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record.
Structure helps to construct a complex data type in more meaningful way.

Structure is used to represent a record. Suppose you want to store record of Student which consists of student name, address, roll number and age.You can define a structure to hold this information.

Defining a structure


struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.

Syntax :

struct structure_name
{
 data_type member1;
 data_type member2;
 .
 .
 data_type memeber;
};

Example of Structure:-

struct Book
{
char name[15];
int price;
int pages;
};

Here the struct Book declares a structure to hold the details of book which consists of three data fields, namely name, price and pages. These fields are called
structure elements or members.

STRUCTURE IN C++ | C++ STRUCTURE(struct) | DEFINE,INITIALIZE WITH EXAMPLE 

Each member can have different data type,like in this case, name is of char type and price is of int type etc. Book is the name of the structure and is called structure
tag.

Declaring Structure Variables


It is possible to declare variables of a structure, after the structure is defined.
Structure variable declaration is similar to the declaration of variables of any other data types. Structure variables can be declared in following two ways.

 Declaring Structure variables separately:-


struct Student
{
char[20] name;
int age;
int rollno;
} ;
struct Student s; //declaring variables of Student

 Declaring Structure Variables with Structure definition:-


struct Student
{
char name[20];
int age;
int rollno;
} s ;

Here s is variables of structure Student. However this approach is not much recommended.

Size of Structure 


The size of structure is the total size of variable declared in the structure.
In the above example the size of structure is

Char name[20] = 20 bytes
Int age= 2 bytes
Int rollno = 2 bytes
Total size of structure is = 20+2+2= 24 bytes

We use sizeof(structure variable) keyword to find the size of structure

Structure Initialization


Like any other data type, structure variable can also be initialized at compile time.

struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
or
struct patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;

Simple c++ example of struct(structute)


#include <iostream>
using namespace std;
struct Student{
   char stuName[25];
   int stuRollNo;
   int stuAge;
};
int main(){
   Student s;
   cout<<"Enter Student Name: ";
   cin.getline(s.stuName, 30);
   cout<<"ENter Student Roll No: ";
   cin>>s.stuRollNo;
   cout<<"Enter Student Age: ";
   cin>>s.stuAge;
   cout<<"Student Record:"<<endl;
   cout<<"Name: "<<s.stuName<<endl;
   cout<<"Roll No: "<<s.stuRollNo<<endl;
   cout<<"Age: "<<s.stuAge;
   return 0;
}


Output:


Enter Student Name: Negan
ENter Student Roll No: 4101003
Enter Student Age: 22
Student Record:
Name: Negan
Roll No: 4101003
Age: 22


Structure in c++ | Declaration, Initialization,Define a structure | Example of c++ structure | My CS Tutorial

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment