Structure (स्ट्रक्चर) in C Language – Definition, Syntax, Initialization and Examples

Introduction to Structure

A structure is a user-defined data type in C language. It is used to group different types of data elements under a single name. A structure is also known as a heterogeneous or non-homogeneous data type because it can contain members of different data types.

स्ट्रक्चर (Structure) C लैंग्वेज का एक यूजर डिफाइन्ड डाटा टाइप है। इसका प्रयोग विभिन्न प्रकार के डाटा एलीमेंट्स को एक ही नाम के अंतर्गत संग्रहित करने के लिए किया जाता है। स्ट्रक्चर को हेटेरोजीनियस या नॉन-होमोजीनियस डाटा टाइप भी कहा जाता है, क्योंकि इसमें विभिन्न डाटा टाइप के मेम्बर्स को एक साथ रखा जा सकता है।

For example, if we want to store the information of a student, we may require the following data:

  • Roll Number (Integer)

  • Name (Character Array)

  • Gender (Character)

  • Fees (Float)

इन सभी डाटा एलीमेंट्स के डाटा टाइप अलग-अलग हैं। यदि हम इन सभी को अलग-अलग वेरिएबल में संग्रहित करते हैं, तो इन्हें एक साथ मैनेज करना कठिन हो सकता है।

Structure provides a convenient way to store all these related data elements together.

स्ट्रक्चर इन सभी संबंधित डाटा एलीमेंट्स को एक साथ संग्रहित करने की सुविधा प्रदान करता है।

The struct keyword is used to define a structure in C language.

C लैंग्वेज में स्ट्रक्चर बनाने के लिए struct कीवर्ड का प्रयोग किया जाता है।

Definition of Structure

A structure is a user-defined data type in C language that groups multiple data elements of different data types under a single name.

स्ट्रक्चर C लैंग्वेज का एक यूजर डिफाइन्ड डाटा टाइप है, जिसमें विभिन्न डाटा टाइप के एक से अधिक डाटा एलीमेंट्स को एक ही नाम के अंतर्गत संग्रहित किया जाता है।

Structure is commonly used to store records of real-world entities such as students, employees, customers, books, and products.

स्ट्रक्चर का प्रयोग सामान्यतः स्टूडेंट, एम्प्लॉयी, कस्टमर, बुक तथा प्रोडक्ट आदि के रिकॉर्ड को संग्रहित करने के लिए किया जाता है।

Declaration of Structure

The general syntax of structure declaration is:

struct structure_name
{
    datatype member1;
    datatype member2;
    datatype member3;
    .
    .
    datatype memberN;
};

स्ट्रक्चर डिक्लेरेशन का सामान्य प्रारूप निम्नलिखित है:

struct structure_name
{
    datatype member1;
    datatype member2;
    datatype member3;
    .
    .
    datatype memberN;
};

Here,

  • struct is a keyword used to define a structure.

  • structure_name is the name of the structure.

  • datatype represents the data type of a member.

  • member1, member2, etc. are the members of the structure.

यहाँ struct एक कीवर्ड है, जिसका प्रयोग स्ट्रक्चर बनाने के लिए किया जाता है। structure_name स्ट्रक्चर का नाम होता है तथा member1, member2 आदि स्ट्रक्चर के मेम्बर होते हैं।

Each member of a structure can have a different data type.

स्ट्रक्चर के प्रत्येक मेम्बर का डाटा टाइप अलग-अलग हो सकता है।

Example of Structure Declaration

struct student
{
    int rollno;
    char name[50];
    char gender;
    float fees;
};

In this example, a structure named student is defined.

इस उदाहरण में student नाम का एक स्ट्रक्चर बनाया गया है।

The structure contains four members:

MemberData TypeDescription
rollnointStores the roll number
namechar[50]Stores the student's name
gendercharStores gender
feesfloatStores fees

इस स्ट्रक्चर में चार मेम्बर हैं। rollno एक integer मेम्बर है, name एक character array है, gender एक character मेम्बर है तथा fees एक float मेम्बर है।

Structure Variable

A structure variable is a variable declared using a structure data type. It is used to store the actual data of the structure.

स्ट्रक्चर वेरिएबल वह वेरिएबल होता है, जिसे स्ट्रक्चर डाटा टाइप की सहायता से डिक्लेयर किया जाता है। इसका प्रयोग स्ट्रक्चर के वास्तविक डाटा को संग्रहित करने के लिए किया जाता है।

Syntax

struct structure_name variable_name;

Multiple structure variables can also be declared in a single statement.

struct structure_name var1, var2, var3;

Example

struct student s1;
struct student s2;

Or,

struct student s1, s2;

In this example, s1 and s2 are structure variables of type student.

इस उदाहरण में s1 तथा s2 दोनों student स्ट्रक्चर टाइप के वेरिएबल हैं।

Each variable can store a separate student's information.

प्रत्येक वेरिएबल में अलग-अलग स्टूडेंट का रिकॉर्ड संग्रहित किया जा सकता है।

Structure Declaration with Variables

A structure can also be declared along with its variables.

स्ट्रक्चर को उसके वेरिएबल्स के साथ भी डिक्लेयर किया जा सकता है।

Syntax

struct structure_name
{
    datatype member1;
    datatype member2;
    datatype member3;
} var1, var2, var3;

Example

struct student
{
    int rollno;
    char name[50];
    char gender;
    float fees;
} s1, s2;

In this example, the structure student and two structure variables s1 and s2 are declared together.

इस उदाहरण में student नाम का स्ट्रक्चर तथा उसके दो वेरिएबल s1 और s2 एक साथ डिक्लेयर किए गए हैं।

Memory Allocation in Structure

A structure definition describes the arrangement of its members. Memory for storing the actual member values is allocated when a structure object or variable is created.

स्ट्रक्चर डिफाइन करने पर उसके मेम्बर्स का प्रारूप तैयार होता है। जब स्ट्रक्चर का वेरिएबल बनाया जाता है, तब उसके मेम्बर्स के वास्तविक डाटा को संग्रहित करने के लिए मेमोरी आवंटित होती है।

The memory required by a structure is based on the sizes of its members and may also include padding bytes added by the compiler for memory alignment.

स्ट्रक्चर द्वारा ली जाने वाली मेमोरी उसके मेम्बर्स के साइज पर निर्भर करती है। कम्पाइलर द्वारा मेमोरी अलाइनमेंट के लिए कुछ अतिरिक्त बाइट्स (Padding Bytes) भी जोड़े जा सकते हैं।

Therefore, the size of a structure is not always exactly equal to the sum of the sizes of all its members.

इसलिए स्ट्रक्चर का कुल साइज हमेशा उसके सभी मेम्बर्स के साइज के योग के बराबर नहीं होता है।

Example

struct student
{
    int rollno;
    char gender;
    float fees;
};

The structure contains:

  • rollno of type int

  • gender of type char

  • fees of type float

When a variable of this structure is created, memory is allocated for its members along with any padding required by the compiler.

जब इस स्ट्रक्चर का वेरिएबल बनाया जाता है, तो इसके मेम्बर्स तथा कम्पाइलर द्वारा आवश्यक padding के लिए मेमोरी आवंटित होती है।

The exact size can be checked using the sizeof operator.

स्ट्रक्चर का वास्तविक साइज sizeof ऑपरेटर की सहायता से ज्ञात किया जा सकता है।

printf("%zu", sizeof(struct student));

Initialization of Structure

When initial values are assigned to the members of a structure variable at the time of its declaration, it is called structure initialization.

स्ट्रक्चर वेरिएबल को डिक्लेयर करते समय उसके मेम्बर्स को प्रारंभिक मान प्रदान करना स्ट्रक्चर इनिशियलाइजेशन कहलाता है।

Syntax

struct structure_name variable_name =
{
    value1,
    value2,
    value3,
    ...
    valueN
};

Example

struct student
{
    int rollno;
    char name[50];
    char gender;
    float fees;
};

struct student s1 = {1010, "Ajay", 'M', 600.60};

In this example, the structure variable s1 is initialized with the following values:

MemberInitial Value
rollno1010
nameAjay
genderM
fees600.60

इस उदाहरण में s1 वेरिएबल के सभी मेम्बर्स को प्रारंभिक मान दिए गए हैं।

The value 1010 is assigned to rollno, "Ajay" is assigned to name, 'M' is assigned to gender, and 600.60 is assigned to fees.

Accessing Structure Members

The members of a structure can be accessed using the dot (.) operator. This operator is also called the member access operator.

स्ट्रक्चर के मेम्बर्स को एक्सेस करने के लिए dot (.) ऑपरेटर का प्रयोग किया जाता है। इसे member access operator भी कहा जाता है।

Syntax

structure_variable.member_name

Example

s1.rollno
s1.name
s1.gender
s1.fees

Here, s1 is a structure variable and rollno, name, gender, and fees are its members.

यहाँ s1 एक स्ट्रक्चर वेरिएबल है तथा rollno, name, gender और fees इसके मेम्बर्स हैं।

Example Program: Structure in C

The following program demonstrates the declaration, initialization, and accessing of structure members.

#include <stdio.h>

struct student
{
    int rollno;
    char name[50];
    char gender;
    float fees;
};

int main()
{
    struct student s1 = {1010, "Ajay", 'M', 600.60};

    printf("Roll Number = %d\n", s1.rollno);
    printf("Name = %s\n", s1.name);
    printf("Gender = %c\n", s1.gender);
    printf("Fees = %.2f\n", s1.fees);

    return 0;
}

Output

Roll Number = 1010
Name = Ajay
Gender = M
Fees = 600.60

Explanation

In this program, a structure named student is defined with four members.

इस प्रोग्राम में student नाम का स्ट्रक्चर बनाया गया है, जिसमें चार मेम्बर्स हैं।

The structure variable s1 is initialized with the values 1010, "Ajay", 'M', and 600.60.

यहाँ s1 वेरिएबल को 1010, "Ajay", 'M' तथा 600.60 प्रारंभिक मान दिए गए हैं।

The dot operator is used to access each member of the structure.

स्ट्रक्चर के प्रत्येक मेम्बर को एक्सेस करने के लिए dot ऑपरेटर का प्रयोग किया गया है।

The printf() function displays the stored values on the screen.

printf() फंक्शन की सहायता से सभी मेम्बर्स की values स्क्रीन पर प्रदर्शित की गई हैं।

Structure Array

An array of structures is used to store multiple records of the same structure type.

स्ट्रक्चर के एक से अधिक रिकॉर्ड को संग्रहित करने के लिए array of structures का प्रयोग किया जाता है।

Syntax

struct structure_name array_name[size];

Example

struct student s[1000];

In this example, s is an array of 1000 structure variables of type student.

इस उदाहरण में s एक array है, जिसमें student स्ट्रक्चर टाइप के 1000 रिकॉर्ड संग्रहित किए जा सकते हैं।

Each array element can store one student's information.

Array का प्रत्येक element एक स्टूडेंट का रिकॉर्ड संग्रहित कर सकता है।

For example:

s[0].rollno
s[1].name
s[2].fees

यहाँ s[0] पहले स्टूडेंट का रिकॉर्ड, s[1] दूसरे स्टूडेंट का रिकॉर्ड तथा s[2] तीसरे स्टूडेंट का रिकॉर्ड प्रदर्शित करता है।

Structure Pointer

A pointer can also be declared to point to a structure variable. A structure pointer stores the address of a structure object.

स्ट्रक्चर वेरिएबल के address को संग्रहित करने के लिए structure pointer का प्रयोग किया जा सकता है।

Syntax

struct structure_name *pointer_name;

Example

struct student s1;
struct student *ptr;

ptr = &s1;

In this example, ptr is a pointer to a structure of type student. It stores the address of the structure variable s1.

इस उदाहरण में ptr एक structure pointer है, जो s1 वेरिएबल का address संग्रहित करता है।

Structure members can be accessed through a pointer using the arrow (->) operator.

स्ट्रक्चर पॉइंटर की सहायता से मेम्बर्स को एक्सेस करने के लिए arrow (->) ऑपरेटर का प्रयोग किया जाता है।

ptr->rollno
ptr->name
ptr->gender
ptr->fees

The arrow operator is equivalent to dereferencing the pointer and then accessing the member.

Arrow ऑपरेटर का प्रयोग pointer को dereference करके उसके member को access करने के समान होता है।

For example:

ptr->rollno

is equivalent to:

(*ptr).rollno

Advantages of Structure

The main advantages of structure are:

  1. It allows different data types to be grouped under a single name.

    यह विभिन्न डाटा टाइप के मेम्बर्स को एक ही नाम के अंतर्गत संग्रहित करने की सुविधा प्रदान करता है।

  2. It is useful for storing records of students, employees, customers, and other entities.

    इसका प्रयोग स्टूडेंट, एम्प्लॉयी, कस्टमर आदि के रिकॉर्ड को संग्रहित करने के लिए किया जाता है।

  3. It makes programs more organized and readable.

    यह प्रोग्राम को अधिक व्यवस्थित तथा readable बनाता है।

  4. It allows multiple records to be stored using arrays of structures.

    Array of structures की सहायता से एक से अधिक रिकॉर्ड संग्रहित किए जा सकते हैं।

  5. Structure pointers can be used to access structure members efficiently.

    Structure pointers की सहायता से स्ट्रक्चर के मेम्बर्स को एक्सेस किया जा सकता है।

Important Points about Structure

  • Structure is a user-defined data type in C language.

  • The struct keyword is used to define a structure.

  • A structure can contain members of different data types.

  • Structure members are accessed using the dot (.) operator.

  • Structure members can be accessed through a structure pointer using the arrow (->) operator.

  • Memory for the structure's members is allocated when a structure object is created.

  • The size of a structure may include padding bytes.

  • An array of structures can be used to store multiple records.

स्ट्रक्चर C लैंग्वेज का एक यूजर डिफाइन्ड डाटा टाइप है। इसे struct कीवर्ड की सहायता से बनाया जाता है। इसमें विभिन्न डाटा टाइप के मेम्बर्स हो सकते हैं। मेम्बर्स को dot (.) ऑपरेटर से तथा structure pointer के माध्यम से arrow (->) ऑपरेटर से एक्सेस किया जाता है।

Conclusion

Structure is an important user-defined data type in C language. It is used to group related data elements of different data types under a single name. Structure provides an efficient and organized way to store and manage records.

स्ट्रक्चर C लैंग्वेज का एक महत्वपूर्ण यूजर डिफाइन्ड डाटा टाइप है। इसका प्रयोग विभिन्न डाटा टाइप के संबंधित डाटा एलीमेंट्स को एक ही नाम के अंतर्गत संग्रहित करने के लिए किया जाता है।

Structure is widely used in programming applications where records of students, employees, customers, books, and other entities need to be stored and processed.

इस प्रकार स्ट्रक्चर का प्रयोग उन प्रोग्रामिंग एप्लीकेशन्स में किया जाता है, जहाँ स्टूडेंट, एम्प्लॉयी, कस्टमर, बुक तथा अन्य रिकॉर्ड को संग्रहित एवं प्रोसेस करना होता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Top High-Paying Tech Skills to Learn in 2025

Complete C Programming Syllabus | Advanced C Roadmap for Students