C Pointers In Hindi

📔 : C 🔗

C में pointers भी एक variable है जो की किसी value के address को point करता है , मतलब किसी variable के memory address को as a value store करते हैं। इन्हे locator या indicator भी कहते हैं।

C usage of pointer

  1. Dynamic memory allocation : pointers का use करके हम malloc() और calloc() function की help से dynamically memory allocate कर सकते हैं।

  2. 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 कराते हैं।

C pointer example

CopyFullscreenClose FullscreenRun
#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;
}
Output
Memory addres of age : 1113507692
Memory addres using pointer : 1113507692
Normal value : 26
Value using pointer : 26

C update pointer value

CopyFullscreenClose FullscreenRun
#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;
}
Output
Before change age : 26
Before change age_ptr : 26
After change age : 90
After change age_ptr : 90

C Void Pointer

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 करता है।

CopyFullscreenClose FullscreenRun
#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;
}
Output
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 अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers