पॉइंटर एक वेरिएबल है जो दूसरे वेरिएबल के एड्रेस को स्टोर करता है।
पॉइंटर्स की विशेषताएं
-
पॉइंटर मेमोरी स्पेस को बचाता है।
-
मेमोरी लोकेशन तक सीधी पहुंच के कारण पॉइंटर का निष्पादन समय तेज होता है।
-
पॉइंटर्स की मदद से, मेमोरी को कुशलता से एक्सेस किया जाता है, यानी मेमोरी आवंटित की जाती है और गतिशील रूप से हटा दी जाती है।
-
पॉइंटर्स का उपयोग डेटा संरचनाओं के साथ किया जाता है।
सूचक घोषित करना
int *p;
इसका मतलब है कि 'p' एक पॉइंटर वेरिएबल है जो दूसरे इंटीजर वेरिएबल का पता रखता है।
सूचक का प्रारंभ
एड्रेस ऑपरेटर (&) का उपयोग पॉइंटर वेरिएबल को इनिशियलाइज़ करने के लिए किया जाता है।
उदाहरण के लिए,
int qty = 175; int *p; p= &qty;

किसी वेरिएबल को उसके पॉइंटर से एक्सेस करना
वेरिएबल के मान को एक्सेस करने के लिए, इनडायरेक्शन ऑपरेटर (*) का उपयोग किया जाता है।
कार्यक्रम
#include<stdio.h>
void main(){
//Declaring variables and pointer//
int a=2;
int *p;
//Declaring relation between variable and pointer//
p=&a;
//Printing required example statements//
printf("Size of the integer is %d\n",sizeof (int));//4//
printf("Address of %d is %d\n",a,p);//Address value//
printf("Value of %d is %d\n",a,*p);//2//
printf("Value of next address location of %d is %d\n",a,*(p+1));//Garbage value from (p+1) address//
printf("Address of next address location of %d is %d\n",a,(p+1));//Address value +4//
//Typecasting the pointer//
//Initializing and declaring character data type//
//a=2 = 00000000 00000000 00000000 00000010//
char *p0;
p0=(char*)p;
//Printing required statements//
printf("Size of the character is %d\n",sizeof(char));//1//
printf("Address of %d is %d\n",a,p0);//Address Value(p)//
printf("Value of %d is %d\n",a,*p0);//First byte of value a - 2//
printf("Value of next address location of %d is %d\n",a,*(p0+1));//Second byte of value a - 0//
printf("Address of next address location of %d is %d\n",a,(p0+1));//Address value(p)+1//
} आउटपुट
Size of the integer is 4 Address of 2 is 6422028 Value of 2 is 2 Value of next address location of 2 is 10818512 Address of next address location of 2 is 6422032 Size of the character is 1 Address of 2 is 6422028 Value of 2 is 2 Value of next address location of 2 is 0 Address of next address location of 2 is 6422029