0

Consider the following code:

#include <iostream>
using namespace std;
void test0(const int &a){
  cout << "const int &a passed *ptra "<< a << endl;
}
void test1(const int &a){
  cout << "const int &a passed ptra "<< a << endl;
}
void test2(const int &a){
  cout << "const int &a passed *a "<< a << endl;
}
void test3(const int &a){
  cout << "const int &a passed a "<< a << endl;
}
void test4(int *a){
    cout << "int *a passed &a "<< *a << endl;
}
void test5(int *a){
    cout << "int *a passed a "<< a << endl;
}
void test6(int *a){
    cout << "int *a passed *a "<< a << endl;
}
int main(){
  int a = 3;
  int *ptra;
  *ptra = 3;
  test0(*ptra);
  //test1(ptra);
  //test2(*a);
  test3(a);
  test4(&a);
  //test5(a);
  //test6(*a);
  //int &b = a;
}

How come (int &a) when passed a becomes a reference? Is it essentially creating an int a and setting its address to the passed a? Wouldn't it make more sense for the resulting reference to point the address of the value of a (if a = 3 points to address 3), where if you passed like so:

void stuff(int &a);
int *ptr;
stuff(ptr);

a would set its address to the value passed (the address pointed to by ptr); What's the difference between a pointer and a reference?

What's more, if you uncomment //int &b = a the program ceases to output anything. Why?

trisimix
  • 101
  • 1
  • 9
  • 3
    Also `*ptra = 3;` invokes *undefined behavior* - `ptra` is not initialized to point at any valid memory – UnholySheep Mar 20 '18 at 17:50
  • As many other things & means different things in different context. Read a textbook. – Slava Mar 20 '18 at 17:50
  • In a declaration, `&` creates a reference. Anywhere else it's used as a unary operator, it's the "address of" operator. This is your confusion. I also think the duplicate isn't quite right in that it's focusing on a tangential question of the OP. – scohe001 Mar 20 '18 at 17:50
  • @scohe001 you are wrong, it could be binary AND – Slava Mar 20 '18 at 17:50
  • `[teach-me]` Please provide minimal sample with just one question, not 2 questions and a sample fit for 10 more. –  Mar 20 '18 at 17:51
  • @Slava meant as a unary operator. Fixed my comment. – scohe001 Mar 20 '18 at 17:51
  • When you declare a type it is not used as an unary operator either. Still confusing. – Slava Mar 20 '18 at 17:52
  • Aaaaand this is why right-aligning your asterisks and ampersands in teaching material is a bad idea. – Lightness Races in Orbit Mar 20 '18 at 18:10
  • @Slava: I'm not sure I'm convinced by this dupe. I mean sort of yes. But the immediate answer is just "it doesn't - this is a different meaning of `&`" and that dupe doesn't really make it clear IMO – Lightness Races in Orbit Mar 20 '18 at 18:11
  • @LightnessRacesinOrbit you can add different dupe with exactly the same problem, but I think if you understand what is a reference you would not ask why "addressof" became one. – Slava Mar 20 '18 at 18:16
  • I didn't understand 100% what a reference was. Thanks all. – trisimix Mar 20 '18 at 18:24

0 Answers0