🔴 Advanced  ·  Lesson 46

Dynamic Structures

Structures dynamically क्यों allocate करें?

आम तौर पर आप structure को fixed variable के रूप में declare करते हैं। पर कभी-कभी program चलने तक पता नहीं होता कि कितने चाहिए — जैसे user जितने students डाले उतने records। Dynamic allocation आपको run time पर malloc से structures बनाने देता है, ठीक उतनी memory इस्तेमाल करते हुए जितनी चाहिए।

उपकरण किसी भी dynamic memory जैसे ही हैं: आरक्षित करने को malloc, छोड़ने को free — साथ में एक नया syntax, arrow operator।

एक struct allocate करना

C Language
#include <stdio.h>
#include <stdlib.h>
struct Student { char name[20]; int marks; };

int main() {
    struct Student *p = malloc(sizeof(struct Student));
    if (p == NULL) return 1;         // hamesha check karein

    p->marks = 95;                    // arrow operator
    printf("Marks = %d\n", p->marks);

    free(p);
    return 0;
}
Output:
Marks = 95

Arrow operator

जब आपके पास struct variable हो तो dot इस्तेमाल करते हैं। जब struct pointer हो तो arrow ->

आपके पासकिससे accessउदाहरण
Struct variabledot .s.marks
Struct का pointerarrow ->p->marks

p->marks बस (*p).marks लिखने का साफ़ तरीका है — पहले pointer का पीछा करें, फिर member तक पहुँचें।

Structures का dynamic array

एक malloc में n structures की जगह माँगें, फिर सामान्य indexing इस्तेमाल करें।

C Language
#include <stdio.h>
#include <stdlib.h>
struct Student { char name[20]; int marks; };

int main() {
    int n = 3;
    struct Student *arr = malloc(n * sizeof(struct Student));
    if (arr == NULL) return 1;

    for (int i = 0; i < n; i++)
        arr[i].marks = (i + 1) * 10;   // dot, kyunki arr[i] ek value hai

    for (int i = 0; i < n; i++)
        printf("Student %d: %d\n", i + 1, arr[i].marks);

    free(arr);
    return 0;
}
Output:
Student 1: 10
Student 2: 20
Student 3: 30
💡 यहाँ dot, वहाँ arrow

पूरे array pointer के साथ आप arr[i].marks लिखते हैं — क्योंकि arr[i] एक struct value है, pointer नहीं। Arrow सिर्फ़ सादे struct pointer जैसे p->marks के लिए है।

Memory free करना

चाहे एक struct allocate किया हो या array, pointer पर एक free पूरा block छोड़ देता है।

C Language
free(arr);      // saare n structures release
arr = NULL;     // achhi aadat: dangling pointer se bachein

आम गलतियाँ

  • Pointer पर dot . इस्तेमाल करना (या सादे variable पर arrow)।
  • इस्तेमाल से पहले malloc को NULL के लिए जाँचना भूलना।
  • free भूलना, जिससे memory leak होता है।
  • Free करने के बाद memory इस्तेमाल करना (dangling pointer)।
🏋️ अभ्यास

एक number n पढ़ें, n Student structures का array allocate करें, हर एक को name और marks से भरें, print करें, फिर array free करें। अलग-अलग n के लिए पुष्टि करें कि चलता है।

सारांश

  • Run time पर structure बनाने को malloc(sizeof(struct ...)) इस्तेमाल करें।
  • Pointer के ज़रिए members को arrow operator -> से access करें।
  • n structs के लिए malloc(n * sizeof(struct ...)) और arr[i] से index करें।
  • Struct value पर ., struct pointer पर -> इस्तेमाल करें।
  • हमेशा malloc जाँचें और जो allocate करें उसे free करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में structure dynamically कैसे allocate करते हैं?
आप struct के size के साथ malloc इस्तेमाल करते हैं और result एक struct pointer में रखते हैं, जैसे struct Student *p = malloc(sizeof(struct Student));। यह heap पर एक structure के लिए memory आरक्षित करता है, जिसे फिर arrow operator से pointer के ज़रिए access करते हैं।
C में arrow operator क्या है?
Arrow operator -> pointer के ज़रिए structure के member को access करता है। जब p किसी struct का pointer है, p->name का मतलब है "उस struct का name member जिस पर p इशारा करता है।" यह (*p).name का संक्षिप्त रूप है।
C में structures का dynamic array कैसे बनाते हैं?
आप count गुणा struct size के साथ malloc call करते हैं, जैसे malloc(n * sizeof(struct Student)), और उसे struct pointer में रखते हैं। फिर सामान्य array indexing जैसे p[i].marks इस्तेमाल करते हैं, और size fixed होने के बजाय run time पर तय हो सकता है।
dot और arrow operators में क्या अंतर है?
Dot . किसी struct variable पर सीधे member access करता है, जबकि arrow -> struct pointer के ज़रिए। Struct value के साथ . और struct के pointer के साथ -> इस्तेमाल करें।
क्या dynamically allocated structures को free करना ज़रूरी है?
हाँ। malloc से मिला हर block काम पूरा होने पर free से छोड़ना ज़रूरी है, चाहे वह एक struct रखे या उनका array। Free करना भूलना memory leak बनाता है जो program जितना ज़्यादा चले उतना बढ़ता है।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.