-3

What does the following do:

double &number = 12.4;

It should assign the reference to variable of type double as 12.4

but isn't a reference to variable an address of a pointer . Why does it work?

double & Getsomenumber()
{
    static double number = 12.4;

    double &anotherNumber = number;

    return anotherNumber;}

2 Answers2

3

That's illegal syntax, there are no references in C.

If you meant C++, then that is also invalid, you can't bind temporaries to non-const references. const double& number = 12.4 would work.

So "Why does it work?" isn't a valid question - it doesn't, not for C, not for C++.

but isn't a reference to variable an address of a pointer

No. A reference is just an alias - you're basically referring to the same variable via a different name.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

What you are doing should actually work this way:

const double& number = 12.4;

Actually you shouldn't be able to create a reference to a const Expression without const.

bash.d
  • 13,029
  • 3
  • 29
  • 42