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


Pointer variable को same data type के साथ define किये जाता है , मैं जो address variable का type होगा pointer variable का भी वही type होगा। Pointer variables को * से define किया जाता है।

हम जानते हैं कि & operator का use किसी variable का memory address देखने के लिए भी किया जाता है। इसी address को हम pointer variables में store कराते हैं ।

C++ pointers example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
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.
  cout << "Memory addres of age : " << &age << endl;
  // Output the memory address using pointer variable.
  cout << "Memory addres using pointer : " << age_ptr << endl;
  
  // now print value.
  cout << "Normal value : " << age << endl;
  cout << "Value using pointer : " << *age_ptr;
  
  return 0;
}
Output
Memory addres of age : 0x7ffe7ced79ec
Memory addres using pointer : 0x7ffe7ced79ec
Normal value : 26
Value using pointer : 26

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 होती है।

C++ update pointer value

pointers का use करके आप , value को update भी कर सकते हो। लेकिन इससे original value भी change होगी।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  // define a void pointer variable.
  int age = 26;
  int* age_ptr = &age;
  cout << "Before change : \n";
  cout << "age : " << age << endl;
  cout << "age_ptr : " << *age_ptr << endl;
  
  // now update the value.
  *age_ptr = 30;
  cout << "After change : \n";
  cout << "age : " << age << endl;
  cout << "age_ptr : " << *age_ptr << endl;
  
  return 0;
}
Output
Before change : 
age : 26
age_ptr : 26
After change : 
age : 30
age_ptr : 30

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 <iostream>
using namespace std;
int main() {
  // define a void pointer variable.
  void* myptr;
  int age = 26;
  myptr = &age;
  cout << myptr;
  
  return 0;
}
Output
0x7fff5497457c

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