Warning: Trying to access array offset on value of type null in /home/u623081920/domains/hindilearn.in/public_html/tutorials.php on line 241

Warning: Trying to access array offset on value of type null in /home/u623081920/domains/hindilearn.in/public_html/tutorials.php on line 244
Structure In Hindi - C++ Programming - Hindilearn

आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
C++ - Structure
  • Structure ये एक अलग-अलग data types का collection होता है |
  • अगर structure का इस्तेमाल करना हो तो 'struct' keyword का इस्तेमाल करते है |
  • Structure ये array के जैसा ही होता है |
  • Array similar data types का collection होता है और Structure different data type का collection होता है |
  • Structure में किये हुए हर variable के decaration को 'member' कहते है |
  • Structure हर एक member के लिए अलग-अलग memory allocate करता है |

Syntax for Structure Definition

struct structure_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
};  

Example for Structure Definition

struct Employee{
    int emp_id;
    char emp_name[30];
    float salary;
};

यहाँ पर example में structure का नाम 'Employee' लिया है और structure के तीन Members है जिनके अलग-अलग तीन data types है | एक 'emp_id' के लिए integer है, एक 'emp_name' के लिए character है, एक float है जो 'salary' के लिए है |

Structure Variable Declaration

Structure का variable declare करने के दो प्रकार है |
  1. जब Structure की definition लिखी जाती तब वहा पर भी Structure variable को लिखा जाता है |
  2. Structure के variable को main() function के अंदर भी किया जाता है |

Syntax for Structure Variable outside of Structure Definition

struct structure_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
}structure_variable(s);  

Example for Structure Variable in main() Function

struct Employee{
    int emp_id;
    char emp_name[30];
    float salary;
}info;

Syntax for Structure Variable in main() Function

struct structure_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
}structure_variable(s);
int main(){
	struct structure_name structure_variable_name;
}		

Example for Structure Variable in main() Function

struct Employee{
    int emp_id;
    char emp_name[30];
    float salary;
};
int main(){
	struct Employee info;
}	

Structure के Members या Elements को access कैसे किया जाता है ?

Syntax for Accesing Structure members
structure_variable_name . member_of_structure = value(optional);
Example for Accesing Structure members
info.emp_id = 10;

Structure Initilization

Structure Initialization के दो प्रकार है |

Type 1 :

struct Employee{
    int emp_id;
    char emp_name[30];
    float salary;
};
int main(){
	struct Employee info;
		
    info.emp_id = 34;
    strcpy( info.emp_name, "Raj Biradar");
    info.salary = 20000.00;
}

Type 2 :

struct Employee{
    int emp_id;
    char emp_name[30];
    float salary;
};
int main(){
	struct Employee info = {34, "Raj Biradar", 20000.00};
}	

Full Example for Structure

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

struct Employee {
    int  emp_id;
    char  emp_name[30];
    float  salary;
}info;

int main(){

info.emp_id = 34;
strcpy( info.emp_name, "Raj Biradar");
info.salary = 20000.50;

    cout<<"Employee id is : "<<info.emp_id<<endl;
    cout<<"Employee name is : "<<info.emp_name<<endl;
    cout<<"Employee salary is : "<<info.salary<<endl;

return 0;
}
Output :
Employee id is : 34
Employee name is Raj Biradar
Employee salary is : 20000.5

Structure working with size(sizeof)

Structure के member अलग-अलग memory allocate करता है और पूरा Structure का size उनके members के जितनी होती है |

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

struct Employee {
    int emp_id;
    char emp_name[20];
    float salary;
};
int main(){
	struct Employee info;

    cout<<"Size of Employee id is : "<<sizeof(info.emp_id)<<" bytes."<<endl;  // size of emp_id
    cout<<"Size of Employee name : "<<sizeof(info.emp_name)<<" bytes."<<endl; // size of emp_name
    cout<<"Size of Employee salary is : "<<sizeof(info.salary)<<" bytes."<<endl; // size of salary
    cout<<"Size of Employee structure : "<<sizeof(info)<<" bytes."<<endl; // size of Employee

return 0;
}

Ouutput :
Size of Employee id is : 4 bytes
Size of Employee name : 20 bytes
Size of Employee salary is : 4 bytes
Size of Employee structure : 28 bytes

Structure Memory Representation

Memory Representation में देखे तो System 32-bit होने के कारण integer का size '4 Bytes' होता है | character का size bracket में दिए हुए size जितना ही होता है और float का size 4 Bytes ही होता है | Structure का size दिए हुए सभी Members के size जितना होता है | for eg. 4 (int) + 20 (char) + 4 (float) = 28 Bytes होता है |


Structure using Pointer

Structure के Members को दो प्रकार से access किया जाता है |
  1. . (dot Operator)
  2. -> (pointer Operator)

Accessing Members using pointer Operator

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

struct Employee {
    int  emp_id;
    char  emp_name[30];
    float  salary;
};
int main(){
	struct Employee info;
	struct Employee *ptr;

	ptr = &info;

    ptr->emp_id = 34;
    strcpy( ptr->emp_name, "Raj Biradar");
    ptr->salary = 20000.00;

    cout<<"Employee id is : "<<ptr->emp_id<<endl;
    cout<<"Employee name is : "<<ptr->emp_name<<endl;
    cout<<"Employee salary is : "<<ptr->salary<<endl;

return 0;
}

< /pre>
Output :
Employee id is : 34
Employee name is Raj Biradar
Employee salary is : 20000.000000

Array of Structure

Syntax for Array Structure Declaration
 struct structure_name structure_variable_name[array_size];

Example for Array Structure Declaration
 struct Employee info[3];

अगर एक से ज्यादा Employee की information store करनी हो तो array का इस्तेमाल करता है |

Example for Structure Without using Array

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

struct Employee{
    int emp_id;
    char emp_name[20];
    float salary;
};

int main(){
    struct Employee info1, info2, info3;

    info1.emp_id=34;
    strcpy(info1.emp_name, "Raj");
    info1.salary = 20000.00;

    info2.emp_id=35;
    strcpy(info2.emp_name, "Maruti");
    info2.salary = 30000.00;

    info3.emp_id=36;
    strcpy(info3.emp_name, "Suraj");
    info3.salary = 40000.00;

    cout<<"Student 1"<<endl;
    cout<<"Employee id is : "<<info1.emp_id<<endl;
    cout<<"Employee name is : "<<info1.emp_name<<endl;
    cout<<"Employee salary is : "<<info1.salary<<endl<<endl;

    cout<<"Student 2"<<endl;
    cout<<"Employee id is : "<<info2.emp_id<<endl;
    cout<<"Employee name is : "<<info2.emp_name<<endl;
    cout<<"Employee salary is : "<<info2.salary<<endl<<endl;

    cout<<"Student 3"<<endl;
    cout<<"Employee id is : "<<info3.emp_id<<endl;
    cout<<"Employee name is : "<<info3.emp_name<<endl;
    cout<<"Employee salary is : "<<info3.salary<<endl;

return 0;
}
Output :
Student 1
Employee id is : 34
Employee name is Raj
Employee salary is : 20000.000000

Student 2
Employee id is : 35
Employee name is Maruti
Employee salary is : 30000.000000

Student 3
Employee id is : 36
Employee name is Suraj
Employee salary is : 40000.000000

Same Example for Structure using Array

#include <iostream.h>
#include <string.h>
using namespace std;

struct Employee{
    int emp_id;
    char emp_name[20];
    float salary;
};
int main(){

    struct Employee info[3];
int i;

    info[0].emp_id=34;
    strcpy(info[0].emp_name, "Raj");
    info[0].salary = 20000.00;

    info[1].emp_id=35;
    strcpy(info[1].emp_name, "Maruti");
    info[1].salary = 30000.00;

    info[2].emp_id=36;
    strcpy(info[2].emp_name, "Suraj");
    info[2].salary = 40000.00;

for(i=0; i<3; i++){
    cout<<"Student "<<i+1<<endl;
    cout<<"Employee id is : "<<info[i].emp_id<<endl;
    cout<<"Employee name is : "<<info[i].emp_name<<endl;
    cout<<"Employee salary is : "<<info[i].salary<<endl<<endl;
}
return 0;
}
Output :
Student 1
Employee id is : 34
Employee name is Raj
Employee salary is : 20000.000000

Student 2
Employee id is : 35
Employee name is Maruti
Employee salary is : 30000.000000

Student 3
Employee id is : 36
Employee name is Suraj
Employee salary is : 40000.000000