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.
Reference Variable किसी existing variable का reference ही है , जिसे & operator की help से define किया जाता है। या कह सकते हैं कि reference variables किसी exist variables के alias होते हैं , फिर दोनों variables में से कोई भी use कर सकते हैं।
string name = "Raju"; string &otherName = name;
तो कुछ इस तरह से reference variables को define करते हैं , हालाँकि इनमे से आप किसी भी variable को access करेंगे तो दोनों का output same ही आएगा।
#include <iostream>
using namespace std;
int main() {
string name = "Rahul Verma";
string &otherName = name;
cout << name << endl;
cout << otherName;
return 0;
}
Rahul Verma Rahul Verma
ध्यान रहे कि दोनों में किसी भी variable में change होने पर वो सभी दोनों variables (normal & reference variables) में भी change होगा।
#include <iostream>
using namespace std;
int main() {
string name = "Rahul Verma";
string &otherName = name;
cout << "Before Change : " << name << endl;
cout << otherName << endl;
// now change value;
name = "Other Name";
cout << "After Change : " << name << endl;
cout << otherName << endl;
return 0;
return 0;
}
Before Change : Rahul Verma Rahul Verma After Change : Other Name Other Name
इसके अलावा & operator का use किसी variable का memory address देखने के लिए भी किया जाता है।
#include <iostream>
using namespace std;
int main() {
string name = "Rahul Verma";
cout << &name;
return 0;
}
0x7ffcdfdbde80
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