C में Pointer क्या है और उसके प्रकारों की संपूर्ण जानकारी हिंदी में

What is Pointer in C in Hindi

Pointers, C Programming language का सबसे महत्त्वपूर्ण और
शक्तिशाली विशेषताओं में से एक है

Pointer एक Variable है जो अन्य Variable के Memory Address को Store करता है यह Variable int, char, function, array, या अन्य प्रकार के हो सकते है।
Pointer की Size Architecture पर निर्भर करती है तब भी 32-bit Architecture में  में एक Pointer की Size 2 byte है 

ऊपर के चित्र मे 'num' नामक Variable का Memory Address 0x7ffcfa8C165c है जिसे *ptr नामक एक Pointer ने Store करके रखा है अतः Pointer वह है जो न केवल एक Variable बल्कि function, constant, array, strings अन्य pointer आदि के Memory address को भी रखता है 

Syntax of Pointers in C in Hindi

Pointer का Syntax और Variable Declaration Syntax समान है लेकिन Pointer को Declare करते समय हम asterisk  (*) Symbol का Use करते है

Syntax:
data_type *ptr;

ऊपर Syntax में
data_type
यह Data का प्रकार है जिसे Pointer point करेगा ये प्रकार int, char, float, आदि हो सकते है

*: 
यह एक pointer को निर्दिष्ट करता है इस Symbol को asterisk कहते है जिसे Pointer के पहले लगाया जाता है

ptr
यह Pointet Variable को दिया गया नाम है 

Example:
int *a;
int *b:

How to Use Pointers in C in Hindi

Pointers के Use को निम्न तीन Steps में बांटते है।

1) Pointer Declaration
2) Pointer Intialization
3) Pointer Deferencing


Pointer Declaration

C में Pointer को Declare करने के लिए हम asterisk (*) Symbol को Variable Name के पहले लगा देते है
Pointer Variable को निम्न तरीके से Declare करते है।

Syntax:
data_type *var_name;

Example:
int *a;
int* a;
ऊपर के Example में दोनो Declaration बराबर है जिसमे  'a' नामक Pointer Variable Declare किया गया है जो एक integer memory के address को रखता है

यदि हम कई Variables एक Statement में Declare करना चाहते है हो हमे बताने के लिए कि ये Variables, Pointer Variables है इन Variables के पहले asterisk Symbol अर्थात (*) लगाना होता है  

Example:
int *m, *n, *o;

ऊपर का Example बताता है कि तीन Variables Pointers  "m", "n" और  "o" Declare किया गया है जिसने एक integer के Memory Address को रखा है।

Pointer Intialization

Pointer Intialization में हम Pointer Variable को प्रारंभिक Value Assign करते है इस कार्य को करने के लिए address-of operator (&) का Use करते है ताकि एक Variable के Memory Address को प्राप्त कर सकें और उसे Pointer Variable में Store कर सकें

Syntax:
data_type *pointer_name = &variable_name;

ऊपर Syntax में 
data-type data का प्रकार है जिसे pointer point करेगा
(*) Asterisk Symbol यह सूचित करता है कि variable एक pointer है

pointer_name,  pointer variable  का नाम है, (&), Address-of operator है तथा variable_name, variable का नाम है जिसके Address को Pointer Store करेगा 

Example:
int num= 23;
int *ptr = # 

ऊपर Example में num एक Integer type variable है जिसे 23 Value Assign किया गया है

int *ptr , Statement का Use करके एक pointer variable, 'ptr' जिसका type int है Declare किया गया है

&num का Use num variable के memory address को प्राप्त करने के लिए किया गया है और उस memory address को ptr pointer को Assign किया जाता है

अब *ptr का Use pointer 'ptr' में Store Memory Address पर उपस्थित Value को Access करने के लिए किया जाता है

Pointer Deferencing

C में Pointer dereferencing एक Pointer द्वारा Point किए जा रहे Memory Address में Store Value को Access करने की प्रक्रिया है

हम pointer dereferencing के Syntax के लिए (*) dereferencing operator का Use निम्न प्रकार से करते है

Syntax:
data_type *ptr;  
*ptr;     

Example:
int a = 15; 
int *ptr;     
ptr = &a;   

ऊपर के Example में एक pointer variable 'ptr' है जिसका type int है यह pointer एक Integer variable 'a' के Memory Address को Store करता है 

जब हम ptr, से dereference करते है तब ptr द्वारा point किया जा रहा Memory Address में उपस्थित Value(15) को Access कर पाते है
Program to show Pointer Declaration, Definition and Dereferencing
#include<stdio.h>
int main() 
{
// Declaration of pointer variable
    
    int *ptr;
    int num= 15;
    
// Definition of pointer variable 
    
    ptr = &num;
    
// Printing the value of num
    
    printf("Value of num variable is : %d\n", num);
   
// Printing the memory address of number
    
    printf("Memory address of num is: %p\n", &num);
    
// Printing the value of number using pointer dereferencing
    
    printf("Value of variable num(by using pointer dereferencing) is  : %d\n", *ptr);
    
// Printing the memory address stored in the ptr
    
    printf("Memory address stored in ptr is : %p\n", ptr);
    
return 0;
}
 
Output:
Value of num variable is : 15

Memory address of num is: 0x7ffcfa8c165c

Value of variable num(by using pointer dereferencing) is : 15

Memory address stored in ptr is : 0x7ffcfa8c165c

Explanation:

1) Declaration of pointer variable:
ऊपर  program मे हमने एक pointer variable `ptr` जिसका Data type `int*` हैं Declare किया है ताकि उसमें एक Integer Variable की memory address को Store कर सकें

2) Declaration of a Integer Variable:
अगले Step में हमने एक integer variable `num` को Declare करते है और उसे 15, Value देकर Assign करके initialize करते है 

3) Definition of pointer variable :
अब हम  integer variable `num' के Memory address को address-of operator `&` का Use करके एक Pointer Variable  `ptr` को Assign करते हैं

4) उसके बाद printf() का Use करके 'num' Variable की value को, num के Memory Address को, pointer dereferencing Use करके पुन:  num की value को तथा अंत में ptr में संगृहीत Memory Address को क्रमश: Print करते है

Types of Pointer in C in Hindi

C programming में pointers को उसके उपयोग, व्यवहार और उद्देश्य के आधार पर कई प्रकार से वर्गीकृत किया जा सकता है 
1) Null pointer
2) Void pointer
3) Wild pointer
4) Dangling pointer
5) Pointer to Pointer
6) Function pointer

Null Pointer

एक Pointer Variable जो Pointer Declaration के समय Null value के साथ Initialize होता है Null Pointer कहलाता है यह Pointer, कोई भी Memory location को Point नहीं करता 

Null Pointer का उपयोग यह सूचित करने के लिए होता है कि Pointer अभी  तत्काल में कोई भी Memory location  को संदर्भित नहीं कर रहा है

C, में constant value NULL का Use एक null pointer को सूचित करने के लिए किया जाता है

Syntax
:
 int *ptr = NULL;
ऊपर Syntax में  *ptr एक Null pointer है क्योंकि इस Null value के साथ Initialize किया गया है

Program for Null Pointer
#include<stdio.h>
int main() 
{
 int *ptr = NULL; 
 
printf("Value at null pointer: %d\n", *ptr);
   
    return 0;
}
 
ऊपर program में एक pointer `ptr` को `NULL`, value के साथ Initialize किया गया है अर्थात यह एक null pointer है
जो कुछ भी Point नही कर रहा है 

जब इस null pointer (*ptr) की Value को printf() की मदद से Print किया गया तब इसका यह अपरिभाषित व्यवहार करने लगता है कुछ स्थिति मे इसका result में segmentation fault करके दिखाता है या program crash हो जाता है
 

Void Pointer (Generic Pointer)

वह pointer, जिसे `void*` keywords का Use करके declare किया जाता है Void Pointer कहलाता है

यह किसी भी प्रकार के Address को Contain कर सकता है 
Void Pointer, विभिन्न Data type जैसे integers, characters, floats, arrays, structures आदि वाले object को  Point  करता है
इस Pointer की Size 2-byte होती है

Syntax:
void *var_name;

Example:
void *ptr;

ऊपर *ptr एक void pointer के रूप में Declare किया गया है जो किसी भी Data type को Point कर सकता है

Program for Void Pointer
#include<stdio.h>
int main()
 {
    int a = 25;
    float b = 6.5; 
    void*ptr;
    ptr = &a;

printf("Value of integer variable is : %d\n",   *(int*)ptr); 
    ptr = &b;

printf("Value of float variable is: %d\n", *(float*)ptr); 
    return 0;
}
 
Output:
Value of integer variable is : 25
Value of float variable is: 904941609

Explanation:
ऊपर के program में एक void pointer (void *ptr) का Use किया गया है जिसमें विभिन्न Data types(int, float) वाले Variables (a,b) के Address को Store किया गया है

उसके बाद उन Addresses में Store values को Access करने और Print करने के लिए  void pointer को विशेष प्रकार (int*, float*) में बदला गया है इसके लिए type casting का प्रयोग करते हैं

Wild pointer

वह Pointer जो किसी भी Address से Initialize नही होता Wild pointer कहलाता है

Wild pointer को Bad pointer के नाम से भी जाना जाता है क्योंकि यह Random memory location के Address को रखता है

Syntax:
data_type *var_name;

Example:
int *ptr;

Program for Wild Pointer
#include<stdio.h> 
int main() 
{
    int *ptr; // Declaration of a wild pointer
    
    printf("Value at wild pointer: %d\n", *ptr);    
    return 0;
} 
 
Output:
Segmentation fault

Explanation:
ऊपर program में हमने
एक pointer `ptr`, नाम से एक wild pointer Declare किया है जिसे Initialize नही किया गया है

उसके बाद 'ptr' wild pointer का Use करके इसके Value को Access करने का प्रयास करते है

ऐसा करने से  Program Crash हो जाता है या एक segmentation fault Result के रूप में प्राप्त होता है

Dangling pointer

Dangling pointer वह है जो ऐसे Memory location को point करता है जो खाली या deallocate हो चुका है

एक Dangling pointer को Access या Use करना समस्या उत्पन्न कर सकता है
क्योंकि इस Pointer द्वारा जिस Memory Address को Point किया जा रहा है, हो सकता है वह Address अन्य उद्देश्य के लिए Use हो चुका है या Valid नही है
 
Syntax:
data_type *var_name;

Example:
int *ptr;


Program for Dangling Pointer
#include<stdio.h> 
#include<stdlib.h> 
int main() 
{
    int *ptr = (int *)malloc(sizeof(int));
    
    *ptr = 15;
    printf("Value before freeing memory: %d\n", *ptr); 
    
    free(ptr);     
    printf("Value after freeing memory: %d\n", *ptr); 
    return 0;
}
 
Output:
Value before freeing memory: 15
Value after freeing memory: 0

Explanation:
1) हम malloc function का Use करके एक Integer के लिए Memory allocate करते है

2) उसके बाद Pointer 'ptr' का Use करके उस Allocated memory में एक Value (15) store करते है

3) अब free function का Use करके उस allocated memory को खाली कर देते है

4) Memory को खाली करने के बाद हम pointer ptr की Value को Access करने का प्रयास करते है जो कि एक dangling pointer बन चुका है

5) एक dangling pointer को Access करने से अक्सर अप्रत्याशित परिणाम प्राप्त हो सकते है या Program Crash हो जाता है  

Pointer to Pointer/Double Pointer

वह Pointer जो अन्य pointer variable के Address को रखता है Pointer to Pointer कहलाता है

इस प्रकार के pointer का Use उस समय करने की आवश्यकता होती है जब आपको एक Pointer Variable को Indirect रूप से Access करना है या Pointer variable को स्वयं ही Modify करना है 

यदि हम Pointer to Pointer के साथ ज्यादा कार्य कर रहे हैं तो Execution धीमा हो जाता है

Syntax:
data_type **ptr_to_ptr;

Example:
int **ptr_to_ptr;

Program for Double Pointer
#include<stdio.h>
int main()
 {
    int a = 10;
    int *ptr1 = &a; 
 //Here ptr2 is double pointer
    int **ptr2 = &ptr1; 
    
    printf("Value of a variable is: %d\n", a);
    
    printf("Value at ptr1: %d\n", *ptr1);
   
    printf("Value at ptr2: %d\n", **ptr2);
   
    return 0;
}
 
Output:
Value of a variable is: 10
Value at ptr1: 10
Value at ptr2: 10

Explanation:
1) ऊपर के program में हमने एक integer variable 'a' नाम से Declare किया है और उसे 15 Assign किया है

2) हमने एक  pointer `ptr1` declare कर उसे `a` का Address Assign किया है  

3) उसके बाद हमने एक double pointer `ptr2` declare कर उसे `ptr1` का Address Assign किया है 

4) हम ptr 1 द्वारा Point किया जा रहा Address की Value को Access करने के लिए `*ptr1` का Use करते है

उसी प्रकार  ptr2 द्वारा Point किया जा रहा Address की Value को Access करने के लिए `*ptr2` का Use करते है
चुकि ptr2 द्वारा Point किया जा रहा Address और ptr1 का Address दोनो वही है अतः ये Address Indirect रूप से 'a' को ही Point करता है
 

Function pointer

एक function pointer वह  pointer है जो एक variable या Data के Address को Point न करके Function के Address को Point करता है

Syntax:
return_type (*pointerName)(parameter_list);

Example:
int (*function_pointer) (int, int):

Program for Function Pointer
#include<stdio.h> 
int sum(int a, int b)
  {
    return a + b;
  }

int main()
{
    // Declaration of a function pointer 
    int (*ptr)(int, int);
       ptr= sum;
    int result = ptr(7, 6);
   
    printf("Result of using function pointer is: %d\n", result);
    
return 0; 
}
 
Output:
Result of using function pointer is: 13

Explanation:
1) program मे हमारे पास एक function `sum` है जो दो numbers को  add करता है

2) हमने एक  function pointer `ptr` declare किया है जो दो 'int' argument लेता है और एक 'int' return करता है 
यह function pointer एक function(sum) को point करता है

3) हमने  `sum` function के Address को  function pointer `ptr` को Assign किया जाता तथा 
 
4) `sum` function  को function pointer `ptr`  का Use करके Call करते है और प्राप्त result को  `result` variable में store करते है
अंत में result को print करते है

Importance of pointer/Uses of Pointer in Hindi

1) Pointers आपको malloc`, `calloc`, और `realloc`, function का Use करके Runtime के दौरान Memory को dynamically allocate करने की अनुमति देता है जिससे Memory Management को बड़ी कुशलता से सक्रिय किया जा सके

2) Pointers के द्वारा सीधे ही Memory location को Access किया जा सकता है जिससे Data की Copy किए बिना बड़ी कुशलता से उसमे बदलाव लाया जा सकता है   

3) Pointers, function को reference के द्वारा parameters Pass करने की सुविधा देता है जो आपको Data जो आपको Data की Copies में कार्य करने के बदले original variable को Modify करने की अनुमति देता है।
यह बड़े Data Structure के लिए उपयोगी है 

4) Pointers arrays और strings के साथ कार्य करने के लिए एक महत्त्वपूर्ण भूमिका निभाता है जो उसके Element को Access करने,traversal, और manipulation की सुविधा दिया है

5) Pointers अनावश्यक Memory के duplication को Avoid करता है और Memory location को पुन: Use करने की अनुमति देता है जिससे Memory का सही तरीके से उपभोग होता 

6) Pointers low-level system programming  कार्यों जैसे Hardware डिवाइसेज के साथ Interface करने, System resources को Manage करने और Device Driver को क्रियान्वित करने के लिए आवश्यक है


Differences between Pointer and Array in Hindi

 Related Post