Derived data types in C in Hindi – Function, Array, Pointer Detailed Explanation और Example Program के साथ

आज हम बात करने वाले हैं Derived Data Types in C के बारे में, जो C programming सीखने वाले हर beginner के लिए एक बेहद ज़रूरी topic है। अगर आपने पहले से int, float, char जैसे basic data types सीखे हैं, तो अब वक्त आ गया है कि आप एक कदम आगे बढ़ें और समझें कि derived data types कैसे काम करते हैं।

इस article में आप सीखेंगे कि Derived Data Type क्या होता है, C language में कौन-कौन से derived data types होते हैं, Function, Array और Pointer को कैसे declare और use करते हैं, इनके बीच क्या अंतर है, और beginners कौन-सी common mistakes करते हैं। साथ ही हम एक practical program भी देखेंगे जिसमें तीनों derived data types को एक साथ use करेंगे।

Derived Data Type क्या होता है?

सोचिए आपके घर में एक बड़ा डिब्बा है जिसमें आप छोटे-छोटे कई डिब्बे रख सकते हैं — जैसे मसालों का डिब्बा जिसमें हल्दी, मिर्च, धनिया अलग-अलग खानों में होते हैं। उसी तरह, C language में basic data types (जैसे int, char, float) से मिलकर बने नए और ज़्यादा powerful types को Derived Data Types कहते हैं।

दूसरे शब्दों में, जो data types किसी और existing data type पर निर्भर होकर बनाए जाते हैं, वे derived data types कहलाते हैं। ये types आपको complex और organized data को memory में store करने की सुविधा देते हैं।

एक छोटा उदाहरण देखें:

// A simple array — derived from int
int numbers[5] = {10, 20, 30, 40, 50};

यहाँ numbers एक array है जो int data type से derived है, और इसमें 5 integer value store कर सकते हैं।

C में Derived Data Types कौन-कौन से हैं?

Derived Data Types in C समझने के लिए पहले उनकी list देख लेते हैं:

  • Function — एक code block जो किसी specific काम को करता है।
  • Array — एक ही data type के multiple values को एक साथ store करने वाली structure.
  • Pointer — किसी variable का memory address store करने वाला special variable।

Note: कुछ textbooks में Structure, Union, और Enum को भी derived types में गिनते हैं, लेकिन इस article में हम मुख्यतः Function, Array, और Pointer पर focus करेंगे।

1. Function in C

Function एक self-contained code block होता है जो एक specific task perform करता है। इसे एक बार define करो और जितनी बार चाहो call करो — यही function की असली ताकत है।

Declaration / Syntax

return_type function_name(parameter_list) {
	// function body
	return value;
}

Function के Components

किसी भी function के ये मुख्य हिस्से होते हैं:

  • Return type — function क्या value वापस करेगा (int, void, float, आदि)
  • Function name — function का नाम (कोई भी valid identifier)
  • Parameters — function को input देने वाले variables
  • Function body — असली काम करने वाला code block
  • Return statement — function से value वापस भेजना

Example Program

#include <stdio.h>

// Calculates and returns the sum of two numbers
int addNumbers(int a, int b)
{
    int result = a + b;
    return result;  // Returns the calculated sum
}

int main(void)
{
    int x = 15, y = 25;
    int sum = addNumbers(x, y);  // Calls the function and stores the returned value

    printf("Sum = %d\n", sum);

    return 0;
}
▶️ Output Result
Sum = 40

Code की Explanation

addNumbers नाम का एक function define किया गया है जो दो int parameters लेता है। main() से इसे x = 15 और y = 25 के साथ call किया गया। Function ने दोनों को जोड़कर 40 return किया जो sum variable में store हुआ।

Function की Properties

  • Code को reusable बनाता है।
  • Program को modular और readable बनाता है।
  • void return type का मतलब function कोई value return नहीं करता।
  • C में main() भी एक function है।
  • Function को call से पहले declare या define किया जाना ज़रूरी है।

2. Array in C

Array एक ऐसा data structure है जो एक ही data type के multiple values को contiguous memory locations में store करता है। जैसे एक row में बैठे हुए छात्र — सब एक ही class के, एक ही line में।

Declaration / Syntax

data_type array_name[size];
// Example:
int marks[5];
Array declaration in C with five integer elements showing index numbers, values and memory addresses

Example Program

#include <stdio.h>

int main(void)
{
    int scores[5] = {85, 90, 78, 92, 88};  // Stores 5 scores
    int i;
    int total = 0;

    for (i = 0; i < 5; i++)
    {
        total += scores[i];  // Adds each score to the total
    }

    printf("Total Score = %d\n", total);
    printf("Average Score = %d\n", total / 5);  // Calculates the average

    return 0;
}
▶️ Output Result
Total Score = 433 Average Score = 86

Code की Explanation

scores नाम का एक integer array declare किया जिसमें 5 values हैं। for loop की मदद से सभी values को total में जोड़ा गया। अंत में total और average print किया गया।

Array की Properties

  • Index हमेशा 0 से शुरू होती है।
  • सभी elements एक ही data type के होते हैं।
  • Array का size declare करते वक्त fix करना होता है (static array में)।
  • Array का नाम उसके पहले element का address होता है।
  • Memory में सभी elements एक के बाद एक (contiguous) होते हैं मतलब एक के बाद एक।

3. Pointer in C

Pointer एक special variable है जो किसी दूसरे variable का memory address store करता है। यह C language का सबसे powerful और थोड़ा tricky concept है।

Real-life analogy: सोचिए किसी दोस्त का घर ढूंढना है। आपके पास उसका address (जैसे “मकान नंबर 45, गली 3”) है — यही address pointer की तरह काम करता है। Address पता होने पर आप उस घर तक पहुँच सकते हैं और अंदर जा सकते हैं। उसी तरह pointer किसी variable का address store करता है और आप उस address के ज़रिए variable की value access कर सकते हैं।

Declaration / Syntax

data_type *pointer_name;
// Example:
int *ptr;
  • & operator — किसी variable का address निकालता है।
  • * operator — pointer के address पर stored value निकालता है (dereference)।
Pointer in C showing pointer declaration, address of a variable, dereferencing and memory representation

Example Program

#include <stdio.h>

int main(void)
{
    int age = 22;          // Stores an integer value
    int *ptr = &age;       // Stores the address of age

    printf("Value of age = %d\n", age);
    printf("Address of age = %p\n", (void *)ptr);  // Prints the memory address
    printf("Value via pointer = %d\n", *ptr);      // Dereferences the pointer

    *ptr = 25;  // Modifies age through the pointer
    printf("Updated age = %d\n", age);

    return 0;
}
▶️ Output Result
Value of age = 22 Address of age = 0x7ffd1234abcd Value via pointer = 22 Updated age = 25

Note: Memory address हर system पर अलग हो सकता है।

Code की Explanation

age variable में value 22 है। ptr pointer उसका address store करता है। *ptr से हम age की value access करते हैं। जब हमने *ptr = 25 लिखा, तो age की actual value भी बदल गई — क्योंकि pointer उसी memory location को point कर रहा था।

Pointer की Properties

  • Pointer declare करते वक्त data type बताना ज़रूरी है।
  • Uninitialized pointer use करना dangerous है (dangling pointer)।
  • Pointer arithmetic संभव है (जैसे ptr++)।
  • Pointer arrays और functions के साथ मिलकर बेहद powerful बन जाता है।
  • NULL pointer किसी भी address को point नहीं करता — यह safe initialization है।

Common Beginner Mistakes

Derived Data Types in C सीखते वक्त beginners अक्सर ये गलतियाँ करते हैं:

Mistake 1: Array index out of bounds

// गलत

int arr[3] = {1, 2, 3};

printf(“%d”, arr[3]); // index 3 exist नहीं करता

// सही

int arr[3] = {1, 2, 3};

printf(“%d”, arr[2]); // last valid index है 2

Mistake 2: Pointer को initialize किए बिना use करना

// गलत

int *ptr;

*ptr = 10; // undefined behavior — crash हो सकता है

// सही

int num = 10;

int *ptr = &num;

*ptr = 20;

Mistake 3: Function call से पहले declaration न करना

// गलत

int main() {

int result = multiply(4, 5); // compiler को पता नहीं है multiply क्या है

return 0;

}

int multiply(int a, int b) { return a * b; }

// सही

int multiply(int a, int b); // prototype पहले declare करो

int main() {

int result = multiply(4, 5);

return 0;

}

int multiply(int a, int b) { return a * b; }

Mistake 4: Array को function में size के साथ pass न करना

// गलत

void printArray(int arr[]) {

int size = sizeof(arr); // यह pointer का size देगा, array का नहीं

}

// सही

void printArray(int arr[], int size) {

// size को अलग parameter की तरह pass करो

}

Mistake 5: & और * operator को confuse करना

// गलत

int x = 5;

int *ptr = *x; // x की value नहीं, address चाहिए

// सही

int x = 5;

int *ptr = &x; // & से address मिलता है

Mistake 6: Function में void return type होने पर value return करना

// गलत

void greet() {

return 1; // void function से value return नहीं होती

}

// सही

void greet() {

printf(“Hello!\n”);

return; // या simply closing brace

}

Practical Program — तीनों Derived Data Types एक साथ

यह program एक student के marks को array में store करेगा, एक function से average निकालेगा, और pointer का भी use होगा:

#include <stdio.h>

// Calculates the average using a pointer to the array
float calculateAverage(int *marks, int count)
{
    int i;
    float total = 0;

    for (i = 0; i < count; i++)
    {
        total += *(marks + i);  // Accesses array elements using pointer arithmetic
    }

    return total / count;
}

int main(void)
{
    int studentMarks[4] = {75, 88, 92, 67};  // Stores marks of 4 subjects
    int *ptr = studentMarks;  // Points to the first array element
    float avg;
    int i;

    printf("--- Student Marks Report ---\n");

    for (i = 0; i < 4; i++)
    {
        printf("Subject %d: %d\n", i + 1, ptr[i]);
    }

    avg = calculateAverage(ptr, 4);  // Passes the array pointer to the function
    printf("Average Marks = %.2f\n", avg);

    return 0;
}
▶️ Output Result
— Student Marks Report — Subject 1: 75 Subject 2: 88 Subject 3: 92 Subject 4: 67 Average Marks = 80.50

Explanation

इस program में तीनों derived data types एक साथ काम कर रहे हैं। studentMarks एक array है। ptr एक pointer है जो उस array को point करता है। calculateAverage एक function है जो pointer और size लेकर average return करता है। Pointer arithmetic *(marks + i) से array के elements access किए गए।

Frequently Asked Questions

Q 1: Derived Data Type और Primitive Data Type में क्या फर्क है?
Ans: Primitive data types वो basic types हैं जो C language में built-in होते हैं जैसे int, float, char, double। Derived data types इन्हीं primitive types से बनाए जाते हैं, जैसे int से बना int[] (array) या int* (pointer)। Primitive types simple होते हैं जबकि derived types complex data को handle कर सकते हैं।
Q 2: क्या C में function को दूसरे function के अंदर define कर सकते हैं?
Ans: नहीं। Standard C (C99/C11) में nested functions allowed नहीं हैं। हर function को globally define करना होता है। GCC कुछ extensions provide करता है लेकिन वो portable नहीं होते। इसलिए हमेशा functions को main() के बाहर define करें।
Q 3: Array और Pointer में क्या relationship है?
Ans: C में array का नाम automatically उसके पहले element का address होता है। इसलिए arr और &arr[0] एक ही चीज़ है। इसी वजह से array को function में pass करते वक्त वो pointer की तरह behave करता है। Pointer arithmetic से आप array के किसी भी element को access कर सकते हैं।
Q 4: NULL pointer क्या होता है और इसका use क्यों करते हैं?
Ans: NULL pointer एक ऐसा pointer है जो किसी valid memory location को point नहीं करता। इसकी value 0 या NULL होती है। जब pointer को initialize करना हो लेकिन अभी कोई address assign नहीं करना हो, तब int *ptr = NULL; लिखते हैं। यह dangling pointer की problem से बचाता है और safe programming practice है।
Q 5: Function में return statement क्या करता है?
Ans: return statement function को खत्म करके control वापस उस जगह भेज देता है जहाँ से function call हुआ था। यह चाहे तो एक value भी वापस भेज सकता है। Function का return type जिस data type का होता है, return की जाने वाली value भी उसी type की होनी चाहिए। जैसे int function में return 10; लिख सकते हैं।

Conclusion ( निष्कर्ष )

आज आपने C में derived data types के बारे में सब कुछ विस्तार से सीखा — Function क्या है और उसके components क्या हैं, Array कैसे काम करता है और memory में कैसे store होता है, और Pointer क्यों खास है और real-life analogy के साथ इसे कैसे समझें। साथ ही आपने comparison table, common mistakes और FAQs के ज़रिए अपनी understanding को मज़बूत किया। अब आप पूरे आत्मविश्वास से कह सकते हैं कि आपको C के derived data types की पूरी समझ है।

अब आप सिर्फ theory नहीं जानते — आपके पास original code examples भी हैं, जिन्हें आप अपने compiler में run करके practice कर सकते हैं। Programming में theory और practice का combination ही असली समझ देता है। तो आज ही अपना compiler खोलिए, ये examples try कीजिए और खुद से नए examples बनाइए। अगर कोई doubt हो तो comment कीजिए — हम मिलकर सीखेंगे! और अगर यह post useful लगी, तो अपने C-learning दोस्तों के साथ ज़रूर share कीजिए।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top