BCA / B.Tech 11 min read

pointer in Hindi | C

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: C Language Notes in Hindi | C language के नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: pointer in Hindi | C - General Topics
  • • अवलोकन: Quick Answer: C language में Pointer एक विशेष variable होता है जो किसी सामान्य data value के बजाय किसी दूसरे variable का memory address (RAM में उसका स्...
  • • मुख्य अवधारणाएं: C pointer, Address dereference arrays functions Hindi, Void pointers pointer,C pointer, Address dereference arrays functions, Void pointers pointer
Quick Answer: C language में Pointer एक विशेष variable होता है जो किसी सामान्य data value के बजाय किसी दूसरे variable का memory address (RAM में उसका स्थान) store करता है। Pointer को * (asterisk) symbol से declare किया जाता है और & (address-of) operator से किसी variable का address प्राप्त किया जाता है। इसका मुख्य उपयोग Call by Reference, dynamic memory allocation और data structures बनाने में होता है।

Pointer क्या है? (What is a Pointer in C?)

जब आप C language में कोई variable बनाते हैं, जैसे int x = 25;, तो कंप्यूटर की RAM में तीन चीजें होती हैं:

  1. Variable Name: x (पहचान के लिए)।
  2. Value: 25 (इसके अंदर store data)।
  3. Memory Address: RAM का specific location (जैसे 0x7ffee4; असली address हर run/system पर अलग होता है), जहाँ यह 4 bytes सुरक्षित हैं।

सामान्य variable सीधे value store करता है, जबकि Pointer variable उस memory address को store करता है। यदि आप variables और data types के basic rules समझना चाहते हैं, तो arrays in C in Hindi और tokens and keywords in Hindi देख सकते हैं।

Advertisement

Pointer का Syntax और मुख्य Operators

Pointer के साथ दो fundamental operators काम करते हैं:

1. Address-of Operator (&)

यह operator किसी भी variable का memory address return करता है। जैसे &x का अर्थ है "Address of x"।

2. Indirection / Dereferencing Operator (*)

जब किसी pointer variable के आगे * लगाया जाता है, तो यह pointer से point किए गए address पर रखी actual value को निकालता है या modify करता है।

int a = 10;
int *ptr;     // ptr is a pointer to an integer
ptr = &a;     // ptr now stores the address of a

printf("Value of a: %d\n", a);         // 10
printf("Address of a: %p\n", &a);      // e.g. 0x7fff5fbff8ac
printf("Value stored in ptr: %p\n", ptr); // same address as &a
printf("Value at *ptr: %d\n", *ptr);   // 10 (Dereferencing)

Pointer Arithmetic (पॉइंटर अंकगणित)

Pointer में साधारण arithmetic नहीं होता; यह data type के size के अनुसार multiply होकर आगे बढ़ता है:

  • यदि int *ptr = 1000 है और 4-byte integer system है, तो ptr + 1 करने पर नया address 1004 होगा (1001 नहीं!)।
  • यदि char *cptr = 2000 है (1-byte character), तो cptr + 1 करने पर 2001 मिलेगा।
  • Pointer में केवल जोड़ना (+), घटाना (-), increment (++) और decrement (--) मान्य हैं। दो pointers को आपस में multiply या divide नहीं किया जा सकता।

विशेष प्रकार के Pointers (Types of Pointers)

BCA और B.Tech परीक्षाओं में pointers के विभिन्न प्रकारों पर direct question पूछा जाता है:

Pointer Type Definition Syntax / Example Risk / Best Practice
NULL Pointer वह pointer जो memory में किसी भी valid address को point नहीं करता (0 पर point करता है)। int *ptr = NULL; Safe initialization practice. Unused pointer को हमेशा NULL रखें।
Wild Pointer ऐसा uninitialized pointer जिसमें कोई random garbage memory address पड़ा होता है। int *ptr; // unassigned बहुत खतरनाक! Program crash या data corruption कर सकता है।
Dangling Pointer वह pointer जो ऐसी memory location को point कर रहा हो जिसे free() कर दिया गया हो। free(ptr); // ptr is now dangling Memory deallocate करने के तुरंत बाद ptr = NULL; सेट करें।
Void Pointer (Generic) बिना किसी specific data type का pointer, जो किसी भी प्रकार के variable का address hold कर सकता है। void *vptr = &a; Dereference करने से पहले explicit Type Casting (*(int*)vptr) अनिवार्य है।

Call by Value vs Call by Reference using Pointers

C language natively केवल Call by Value support करती है, लेकिन Pointers का उपयोग करके हम Call by Reference achieve करते हैं:

Feature Call by Value Call by Reference (Using Pointers)
Arguments Passed Variable की actual value की copy pass होती है। Variable का memory address (&var) pass होता है।
Memory Consumption नया duplicate variable बनता है (Memory overhead अधिक)। केवल address pass होता है (Fast और memory efficient)।
Changes in Function Function के अंदर किए गए changes original variables पर प्रभाव नहीं डालते। Function के अंदर किए गए changes सीधे caller के original variables को बदल देते हैं।
Practical Example Math calculation जहाँ original input सुरक्षित रखना हो। Swap functions, arrays modification, large structures passing.

विस्तृत तुलना के लिए आप call by value and call by reference in Hindi पढ़ सकते हैं।

Dynamic Memory Allocation में Pointer की भूमिका

Compile time पर array का size fix होता है। यदि runtime पर user की आवश्यकता के अनुसार memory चाहिए, तो pointer के साथ malloc() और free() functions का उपयोग किया जाता है। इस बारे में detail के लिए malloc and calloc in C in Hindi देखें:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr;
    int n = 5;

    // Allocate memory dynamically for 5 integers
    arr = (int*) malloc(n * sizeof(int));

    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Assign and print values
    for (int i = 0; i < n; i++) {
        arr[i] = (i + 1) * 10;
        printf("arr[%d] = %d at %p\n", i, arr[i], &arr[i]);
    }

    // Release memory to avoid memory leaks
    free(arr);
    arr = NULL;
    return 0;
}
Advertisement

Worked Code Example: Swapping Two Numbers using Pointers

नीचे एक क्लासिक practical C program दिया गया है जो Call by Reference (Pointers) से दो संख्याओं की values आपस में बदलता है:

#include <stdio.h>

// Function prototype taking integer pointers as arguments
void swap(int *x, int *y) {
    int temp;
    temp = *x;   // temp gets the value at address x
    *x = *y;     // value at address y copied to address x
    *y = temp;   // temp value copied to address y
}

int main() {
    int num1 = 45, num2 = 90;

    printf("Before Swap: num1 = %d, num2 = %d\n", num1, num2);

    // Passing addresses of num1 and num2
    swap(&num1, &num2);

    printf("After Swap:  num1 = %d, num2 = %d\n", num1, num2);
    return 0;
}

Output:
Before Swap: num1 = 45, num2 = 90
After Swap: num1 = 90, num2 = 45

विद्यार्थी अक्सर क्या गलतियां करते हैं? (Common Student Mistakes)

  • Uninitialized pointer को dereference करना: छात्र int *p; *p = 50; लिख देते हैं। p में किसी valid variable का address डाले बिना *p access करने से Segmentation Fault (Core Dumped) error आती है।
  • Pointer declaration में * और multiplication में भ्रम: Data type के साथ int *p; pointer declaration है; expression में *p dereference है; और a * b arithmetic multiplication है।
  • free() करने के बाद pointer को दोबारा use करना: Memory free करने के बाद यदि pointer को NULL नहीं किया गया, तो वह Dangling Pointer बन जाता है जिससे unexpected bugs आते हैं।

Exam के लिए Important Questions

Short Questions (2 से 5 अंक)

  1. C language में Pointer क्या है? इसका basic declaration syntax लिखिए।
  2. Address-of operator (&) और Dereference operator (*) में क्या अंतर है?
  3. NULL Pointer और Void Pointer को उदाहरण के साथ स्पष्ट कीजिए।
  4. Dangling Pointer क्या है और इससे कैसे बचा जा सकता है?
  5. यदि int *ptr = 2000; है, तो ptr + 2 की value क्या होगी (यदि sizeof(int) = 4)?

Long Questions (10 से 20 अंक)

  1. C Programming में Pointers के सिद्धांत, महत्व और Pointer Arithmetic के नियमों का detail में वर्णन कीजिए।
  2. Call by Value और Call by Reference की तुलना कीजिए। C भाषा में दो variables की values swap करने का program लिखिए।
  3. Dynamic Memory Allocation क्या है? malloc(), calloc(), realloc() और free() functions का उपयोग Pointers के साथ समझाइए।

FAQs

Q1: 32-bit और 64-bit systems में pointer का size कितना होता है?

Pointer variable केवल memory address store करता है। इसलिए 32-bit architecture पर pointer का size आमतौर पर 4 bytes और 64-bit architecture पर आमतौर पर 8 bytes होता है, चाहे वह char*, int*, या double* हो।

Q2: क्या Pointer to Pointer (Double Pointer) संभव है?

हाँ, int **pptr; एक double pointer होता है जो किसी अन्य pointer variable का memory address store करता है। इसका उपयोग dynamic 2D arrays और complex data structures में होता है।

Q3: Array और Pointer में क्या संबंध है?

C भाषा में किसी array का नाम (जैसे arr) उसके पहले element के address (&arr[0]) को दर्शाता हुआ एक constant pointer होता है। *(arr + i) लिखना arr[i] लिखने के बराबर है।

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

Quick Answer: C language में Pointer एक विशेष variable होता है जो किसी सामान्य data value के बजाय किसी दूसरे variable का memory address (RAM में उसका स्थान) store करता है।
C Language Notes in Hindi | C language के नोट्स हिंदी में के पाठ्यक्रम में pointer in Hindi | C एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको pointer in Hindi | C के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement