Artikel Terbaru

Pointers

Introduction to C Pointers


Introduction to C Pointers 
Pointers वो variables होते है जो दूसरे variables के address को store करते है। जैसा की आपको पता है हर variable का memory में एक unique address होता है। ये address hexadecimal form में होता है। इसलिए इस address को आप किसी normal variable में store नहीं कर सकते है।



किसी भी variable के address को store करवाने के लिए आप pointer variable create करते है। मान लीजिये आपने एक variable create किया है। इस variable का नाम Age है और आपने इसमें 55 value assign करवायी है।



इस variable की memory में location (address) 21F है। अब यदि आप चाहे तो इस address को एक pointer variable में store करवा सकते है। 


Advantages of Using Pointers 

  1. Pointers की मदद से आप dynamically memory allocate कर सकते है। 
  2. Pointers की मदद से आप data structures (linked-list, stack, queue) create कर सकते है।
  3. Pointers use करने से program का execution time कम हो जाता है। 
  4. Pointers की मदद से आप functions से एक से अधिक values return कर सकते है। 
  5. Pointers की मदद से argument passing के दौरान आप variable की copy के बजाय original variable पर काम कर सकते है। 
  6. Pointers के द्वारा large data को search और sort करना बहुत आसान होता है। 

यदि pointers को properly use ना किया जाये तो इसके कुछ dis-advantages होते है। 

Disadvantages of Using Pointers

  1. कई बार pointers की वजह से program में ऐसी error आ जाती है जिसे diagnose करना बहुत difficult होता है। 
  2. Pointers की वजह से memory में leaks create हो जाते है। 
  3. यदि run time के दौरान pointers को hold करने के लिए extra memory ना हो तो program crash हो जाता है। 
  4. Pointers की मदद से restricted memory को access किया जा सकता है। 

Working With Pointers 

यदि आपको एक बार ठीक से समझ आ जाये तो pointers के साथ काम करना बहुत ही आसान है। C language में pointers के साथ काम करने के 2 steps है। 
  1. Declaring a pointer 
  2. Initializing a pointer 

Declaring  Pointers 

C में pointers को declare करने के लिए सबसे पहले आप data type declare करते है। ऐसा इसलिए किया जाता है क्योंकि int type का pointer variable सिर्फ int type के variables का ही address store कर सकता है।      


data_type *pointer_name;
    
इसके बाद आप asterisk (*) operator को declare करते है। इस operator के बाद आप pointer का unique नाम declare करते है।

int *int_pntr;               //Pointer for integer variable 

double *dbl_pntr;       //Pointer for double variable

char *chr_pntr;           //Pointer for char variable  

Initializing Pointers 

C में pointers initialize करने के लिए सबसे पहले आप pointer variable को लिखते है। (Initialization part में pointer variable को बिना asterisk operator (*) के लिखा जाता है।) इसके बाद assignment operator (=) लिखा जाता है।

pntr_variable = &variable_name;
  
इसके बाद आप ampersand operator (&) (इसे address-of operator भी कहा जाता है।) define करते है। इस operator के बाद आप बिना कोई space दिए variable का नाम लिखते है। 

int age = 55;               //Declaring a variable 

int *age-pntr;             //Declaring a pointer

age-pntr = &age;       //Initializing a pointer 

Operators Used with Pointers 

  1. & (Address-of ) operator - ये operator variable के address को point करता है। इससे से आप किसी भी variable का address प्राप्त कर सकते है। 
  2. * (Value-at) operator - ये operator सिर्फ pointer variables के साथ काम करता है। ये operator किसी particular address पर store की गयी value को represent करता है। 

Example 

#include<stdio.h>

int main()
{
    int age = 55;
    int *pntr;
    pntr = &age;

    printf("Address of age variable is : %d\n",pntr);

    printf("Value of age variable is : %d",*pntr);

}
     
इस उदाहरण में pointers के द्वारा एक variable का address और उसकी value print करवाई गयी है। ये pointers की working का एक बहुत ही simple उदाहरण है।

Address of age variables is : -1074204644;
Value of age variables is : 55 

Post a Comment

0 Comments