Home>

I don't know the principle of pointer passing by reference, so I would appreciate it.

====================================
[When passing a normal variable]
void main () {
int a = 10;
hoge (a);
}

void hoge (int&a) {
std :: cout<<a<<std :: endl;
}

====================================

====================================
[When passing a pointer]
void main () {
int b = 10;
int * a =&b;
hoge (* a);
}

void hoge (int&a) {
std :: cout<<a<<std :: endl;
}

====================================

In the case of the first [Normal], it can be done with hoge (a),
In the case of a pointer, I am not sure where the pointer is passed to the function.
When [Normal] is passing a real value, why would you pass that pointer when referring to [Pointer]?

  • Answer # 1

    Hello.

    The declaration statementint * a;is a pointer declaration, but* aappearing in an expression means "where pointer a points".hoge (* a);is an expression, not a declaration statement, so this* ais a variable b because it is "where pointer a points".

  • Answer # 2

      

    Why do you pass a pointer when referring to [pointer]?

    I don't pass a pointer pointer.
    If so

    int b = 10;
    int * a =&b;
    hoge (&a);// pointer pointer


    It becomes.