confusion about reference and pointer

int myvar = 5;
int * ptr = & myvar;
int & rfr = myvar;

void funS(int Spar){};
void funP(int * Ppar){};
void funR(int & Rpar){};

now what do you say about these calls

funS(myvar);
funS(* ptr);
funS(& rfr);
funS(rfr);

funP(ptr);
funP(& myvar);
funP(rfr);
funP(& rfr);

funR(myvar);
funR(& myvar);
funR(ptr);
funR(* ptr);

please tell me which ones are illegal, deprecated, not-recommended, O.K.

and if there could be some other forms too
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funS(myvar);
funS(* ptr);
funS(& rfr);
funS(rfr);

funP(ptr);
funP(& myvar);
funP(rfr);
funP(& rfr);

funR(myvar);
funR(& myvar);
funR(ptr);
funR(* ptr);


http://rextester.com/MZILH94522
Last edited on
You can try writing a program to do those, and see which ones the compiler thinks are illegal.

A reference is a way of referring to another variable by giving it another name.

1
2
3
string george = "Hi! I'm George!";

string& jorge = george;

Both 'george' and 'jorge' are actually the same variable. You've just managed to give them different names.


A pointer is like a middleman. He just shows you where the one you want is.

1
2
3
string george = "Hi! I'm George!";

string* i_know_where_george_is = &george;

While 'george' is the variable that we want to play with, the 'i_know_where_george_is' variable helps us find him.


Finally, the type of a thing matters. Some things can be automatically converted to another thing:

- a variable can be assigned to a reference: int & ref = var;

Other things you cannot convert:

- a pointer != a referece: int & ref = ptr; // NOO!

You must first follow the pointer: int & ref = *ptr; // YES!

Hope this helps.
Thankyou guys yes this does help a lot I will figure out timely when it's illegal but these points are helpful
Topic archived. No new replies allowed.