If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
C में pointers भी एक variable है जो की किसी value के address को point करता है , मतलब किसी variable के memory address को as a value store करते हैं। इन्हे locator या indicator भी कहते हैं।
Dynamic memory allocation : pointers का use करके हम malloc() और calloc() function की help से dynamically memory allocate कर सकते हैं।
Arrays, Functions and Structures : pointers का use arrays, functions और structures में भी किया जाता है , जिससे code भी reduce होता है और performance भी improve होती है।
Pointer variable को same data type के साथ define किये जाता है , means जो address variable का type होगा pointer variable का भी वही type होगा। Pointer variables को * से define किया जाता है।
हम जानते हैं कि & operator का use किसी variable का memory address देखने के लिए भी किया जाता है। इसी address को हम pointer variables में store कराते हैं।
#include <stdio.h>
int main() {
// define a int variable.
int age = 26;
// A pointer variable that stores the address of age.
int* age_ptr = &age;
// Output the memory address.
printf("Memory addres of age : %i\n", &age);
// Output the memory address using pointer variable.
printf("Memory addres using pointer : %i\n", age_ptr);
// now print value.
printf("Normal value : %i\n", age);
printf("Value using pointer : %i", *age_ptr);
return 0;
}
Memory addres of age : 1113507692 Memory addres using pointer : 1113507692 Normal value : 26 Value using pointer : 26
#include <stdio.h>
int main() {
// define a int variable.
int age = 26;
int* age_ptr = &age;
// now print value.
printf("Before change age : %i\n", age);
printf("Before change age_ptr : %i\n", *age_ptr);
// now make changes in any variable / pointer variable.
*age_ptr = 90;
printf("After change age : %i\n", age);
printf("After change age_ptr : %i\n", *age_ptr);
return 0;
}
Before change age : 26 Before change age_ptr : 26 After change age : 90 After change age_ptr : 90
void pointer एक general-purpose pointer है जिसका जिसका use किसी भी type के address को store करने के लिए किया जाता है। Initialization के समय इसे किसी particular data type के साथ नहीं define किया जाता बल्कि void keyword के साथ define किया जाता है।
normally हम , int type के variable address करने के लिए int type का ही pointer declare करते हैं , और string type के variable address को store करने के लिए string type का , लेकिन void pointer की help से हम इस problem को resolve करने में काफी हद तक help करता है।
#include <stdio.h>
int main() {
// define a void pointer variable.
void* myptr;
int age = 26;
// now you can assign any type of value .
myptr = &age;
printf("Address : %i", myptr);
return 0;
}
Address : 197218284
ध्यान रहे void pointer किसी भी type की value का सिर्फ address store करता है value नहीं , अगर आप void pointer variable से value access करेंगे तो कुछ इस तरह से error आएगी।
void* myptr;
int age = 26;
myptr = &age;
// access void pointer variable's value.
printf("Address : %i", *myptr);
Error :
test.c: In function 'main':
test.c:8:26: warning: dereferencing 'void *' pointer
8 | printf("Address : %i", *myptr);
| ^~~~~~
test.c:8:26: error: invalid use of void expression
I Hope, आपको C language में pointers अच्छे से समझ आ गया होगा।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook