What does a statement like "int& p = a" mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What does a statement like "int& p = a" mean?

This is the code from one test: //------------------------------ #include <iostream> using namespace std; int main() { int a = 5; int& p = a; p-=1; //cout << a; return 0; } //------------------------------ I can't understand what does the statement "int& p = a ;" do. It doesn't compile without the next one "p-=1;". Would somebody explain it's sense to me? My Eng. is not good, sorry.

9th Mar 2020, 11:13 AM
Сергей Куликов
Сергей Куликов - avatar
4 Answers
+ 2
That is a reference. You are creating another variable name that you can use instead of a. So you can use either a or p for the same effect.
9th Mar 2020, 11:21 AM
HonFu
HonFu - avatar
+ 2
Сергей Куликов there are ton of resources online to learn any language you want(including sololearn). For best results I would suggest a culmination of them and sololearn
9th Mar 2020, 11:37 AM
Arsenic
Arsenic - avatar
+ 1
int& p= a; binds the integer reference p to a. The address of the variable p is completely unmodified. It simply means that all uses (references) of a actually use the value assigned to a.
9th Mar 2020, 11:24 AM
Arsenic
Arsenic - avatar
+ 1
Thanks to all for the help. I'm in a trip now and don't have my c++ book whith me. Did not find anything about that in Sololearn c++ course.
9th Mar 2020, 11:35 AM
Сергей Куликов
Сергей Куликов - avatar