Home > Software design >  Difference between "&" in "int &a = b;" vs "int c = &d;" [duplicate]
Difference between "&" in "int &a = b;" vs "int c = &d;" [duplicate]

Time:09-29

I'm just starting to learn about c .

And I want to know about this

int g = &h;

what does "&h" mean in there?

I know that in here

int &e = f;

means "e" is referencing to "f".

So what are the difference between "&" in something like this

int &a = b;
int c = &d;

CodePudding user response:

& on the left-hand-side of = means reference. On the right-hand-side, it means 'the memory address of'

e.g.

int g = &h;

Is almost certainly an error. It says, "create an integer, g, and set its value equal to the memory address of variable h.

Instead, you can have:

int *g = &h;

It says, "create a pointer to an integer, g, and set its value equal to the memory address of variable h. g points to h.

And, as you said:

int &e = f;

means e is now a reference to f

  •  Tags:  
  • c
  • Related