2

How to pass a pointer variable as a reference parameter?

I have an additional question about this question.

The answerer in the question is using int*& as the parameter data type, but I don't understand the sequence of the pointer and reference data type.

In my opinion, int&* looks more practical to me, so I tried to compile with that, but it wasn't allowed.

I wanna understand clearly about this logic.

I thought int*& a like int*(int& a), so it's like a pointer of a reference, but apparently, it was just right the opposite.

Why can't I use int&* as the meaning of a reference of a pointer?

TYFA
  • 313
  • 1
  • 10
  • 2
    Read from right to left: `int*&` is a reference to a pointer to int. `int&*` would be a pointer to a reference to int (which is illegal and explained here: https://stackoverflow.com/questions/4632762/why-are-pointers-to-a-reference-illegal-in-c) – UnholySheep Mar 23 '19 at 20:07

3 Answers3

2

C++ types are name-centered. int*&foo is how you should think of it.

First, we have &foo -- foo is a reference. A reference to what? *&foo -- a pointer. A pointer to what? int*&foo an integer.

The original C type rules where even intended to "demonstrate how they are used". So int *foo is short hand for int = *foo being a valid expression, or *foo makes an int. The & doesn't quite work that way.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
2

Clockwise/Spiral Rule

There are three simple steps to follow:

  1. Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or [] => Array X size of... or Array undefined size of... (type1, type2) => function passing type1 and type2 returning... * => pointer(s) to...
  2. Keep doing this in a spiral/clockwise direction until all tokens have been covered.
  3. Always resolve anything in parenthesis first!
               +---------+  
               | +-----+ |
               | | +-+ | |
               | | ^ | | |
           int * & t ; | |
            ^  ^ ^   | | |
            |  | +---+ | |
            |  +-------+ |
            +------------+

Stating from t, so:

t is ...

Then we see ;, thus keep going and see &, so

t is a reference to ...

Then we see *

t is a reference to a pointer to ...

Then we see int:

t is a reference to a pointer to an int

That makes perfect sense. Now let's try the other one:

               +---------+  
               | +-----+ |
               | | +-+ | |
               | | ^ | | |
           int & * t ; | |
            ^  ^ ^   | | |
            |  | +---+ | |
            |  +-------+ |
            +------------+

Stating from t, so:

t is ...

Then we see ;, thus keep going and see *, so

t is a pointer to ...

Then we see &

t is a pointer to a reference to ...

Here we immediately run into a problem. You can't have a pointer to a reference. References don't have addresses.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

Because int&* doesn't really make sense, considering what the notation means.

  • int: a data type
  • x *: a pointer to data type x
  • x &: a reference to data type x

We can combine them in the following common forms:

  • int *: a pointer to an int
  • int &: a reference to an int

I will pause to say that hopefully you know the difference between passing by reference and by pointer. If not, see the link.

However, we have another combination we can try:

  • int *&: a reference to a pointer to an int.

We can use a int *& like this: in caller:

int * myInt= 0;
myfunction(myInt);

in myFunction(int *& myInt):

int i;
myInt = &i; 

myInt int in the caller will be the address of i

NOTE: setting myInt to the address of a local variable is not a good idea, as the minute myFunction() ends, the data stored there will be destroyed. This is just for example purposes only

Finally, there is the question at hand: int&*

  • int &*: a pointer to a reference to an int

Now, I propose this question to you: If we have a reference to an int, why would we need a pointer? Answer: we don't. It doesn't make sense to.

So, in short: int*& and int&* are two different things. The first makes sense if you break it down, the second one doesn't.