Pointers and constants
Pointer
C भाषा में pointer एक प्रकार का variable होता है जो दूसरे वेरिएबल के address को स्टोर करता है। यह variable प्रकार int, char, array, function या किसी अन्य pointer का हो सकता है। pointer का आकार architecture पर निर्भर करता है। हालांकि, 32-bit architecture में एक पॉइंटर का आकार 2 byte है|
पॉइंटर से दो ओपेरटर सम्बंधित होते है |
- &(ampersand operator) जिसे हम एड्रेस ऑफ कहते हैं |
- *(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 को निष्क्रिय करने के लिए किया जाता है।
- int *a;//pointer to int
- 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 :
- By Const Keyword
- 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
- Integer constants
- Real or Floating-point constants
- Octal & Hexadecimal constants
- Character constants
- String constants
- Backslash character constants