Pointers and constants

Pointers and constants

Pointer

C भाषा में pointer एक  प्रकार का  variable होता है जो दूसरे वेरिएबल के address को स्टोर करता है। यह variable प्रकार int, char, array, function या किसी अन्य pointer का हो सकता है। pointer का आकार architecture पर निर्भर करता है। हालांकि, 32-bit architecture में एक पॉइंटर का आकार 2 byte है|

पॉइंटर से दो ओपेरटर सम्बंधित होते है |

  1. &(ampersand operator) जिसे हम एड्रेस ऑफ कहते हैं |
  2. *(pointer of indirection) जिसे हम वैल्यू ऑफ कहते हैं |
int n = 50;   
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.

Declaring pointer in c:

C भाषा में pointer को * (asterisk चिह्न) का उपयोग करके घोषित किया जा सकता है। इसे indirection pointer के रूप में भी जाना जाता है जिसका उपयोग एक pointer को निष्क्रिय करने के लिए किया जाता है।

  1. int *a;//pointer to int
  2. char *c;//pointer to char
    #include<stdio.h>  
    int main(){  
    int number=50;    
    int *p;      
    p=&number;//stores the address of number variable    
    printf(“Address of p variable is %x \n”,p); // p contains the address of the number therefore printing p gives the address of number.     
    printf(“Value of p variable is %d \n”,*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.    
    return 0;  
    }

    Pointer to array

    int arr[10];  
    int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.  
    

    Pointer to a function

    void show (int);  
    void(*p)(int) = &display; // Pointer p is pointing to the address of a function

     

    Pointer to structure

struct st {  
    int i;  
    float f;  
}ref;  
struct st *p = &ref;  

Usage of pointer 

1) Dynamic memory allocation:- C language  में, हम dynamically रूप से malloc() और calloc() फ़ंक्शन का उपयोग कर मेमोरी आवंटित कर सकते हैं जहां पॉइंटर का उपयोग किया जाता है।

2) Arrays, Functions और Structures:-C language  में पॉइंटर्स का उपयोग arrays, functions और structures में व्यापक रूप से किया जाता है। यह कोड को कम करता है और performance में सुधार करता है।

 

 

Constants

Constant का normal meaning  स्थिर होता है, मतलब जिसको हम change  नहीं कर सकते और variable का मतबल परिवर्तनशील होता है, मतलब जिसकी वैल्यू को हम चेंज कर सकते है | ठीक उसी तरह c language में भी हम ऐसे ही दो तरह के variable पढ़ते है एक नार्मल वेरिएबल और एक Constant वेरिएबल |

Constant variable ऐसे वेरिएबल होते है जिसको प्रोग्राम में एक बार declare  करने के बाद पुरे पprogram के execution के दौरान उसकी value में कोई भी changes नहीं कर सकते |

In other words, Constant वह वेरिएबल होता है जो डिक्लेअर होने के बाद ुपूरे प्रोग्राम में उसे बदला नहीं जा सकता है | Constant भी एक नार्मल  तरह ही होता है लेकिन इसमें बस थोड़ा सा डिफ्रेंस यह होता ह की कांस्टेंट वेरिएबल को एक बार डिक्लेअर करने के बाद  के रन करने  भी बदलाव नहीं किया जा सकता है |

How to use constant :

  1. By Const Keyword
  2. By #define Preprocessor Directive
1. By Const Keyword:

Const एक keyword है जिसका उपयोग कर, हम Constants टाइप के वेरिएबल को डिक्लेअर करते है और ऐसे वेरिएबल की वैल्यू एक बार declare करने के बाद पुरे प्रोग्राम के execution के दौरान चेंज नहीं कर सकते |

syntax:   const data_type variable_name;

Example: 

const float one=4.33;

Const कीवर्ड का उपयोग हम किसी भी data types  के साथ कर सकते है और जब हम Const कीवर्ड का उपयोग करते हुए कोई वेरिएबल बनाते है तो उस वेरिएबल के डिक्लेरेशन के टाइम जो वैल्यू असाइन कर दिया जाता है वही वैल्यू पुरे प्रोग्राम के रन होने तक रहता है उसे कभी भी चेंज नहीं किया जा सकता |

#include<stdio.h>    
int main()
{  
 const float one = 4.33;    
 printf("The value of one is : %f",one);    
 return 0;  
}

 

2.By #define Preprocessor Directive:

Constants को #define के द्वारा भी डिक्लेअर कर सकते है|

#include<stdio.h>    
#define PI=3.14    // Declaration of Constant 
int main()
{    
 int r;
 float s;
 printf("Enter the radius of circle : ");
 scanf("%d",&r);
 s=r*r*PI;
 printf("The radius of circle is: %f",s);    
 return 0;  
}     

Types of constants

  1. Integer constants
  2. Real or Floating-point constants
  3. Octal & Hexadecimal constants
  4. Character constants
  5. String constants
  6. Backslash character constants

 

Previous articleControl Structures
Next articleWhat is Operators & its Types and Expression

LEAVE A REPLY

Please enter your comment!
Please enter your name here