mu-777
10/19/2015 - 11:57 PM

pointer_test.cpp

#include <iostream>

using namespace std;

void val(int i) { // 値渡し
    cout << i << endl;  // 0
    cout << &i << endl; // 0x7ffd066ebfac
    i = 1;
}

void ref(int &i) { // 参照渡し
    cout << i << endl;  // 0
    cout << &i << endl; // 0x7ffd066ebfc4
    i = 2;
}

void ptr(int *i) { // ポインタ渡し
    cout << i << endl;  // 0x7ffd066ebfc4
    cout << *i << endl; // 2
    *i = 3;
}

int main() {
    // your code goes here
    int a = 0;
    int *p = &a;
    cout << p << endl;  // 0x7ffd066ebfc4 <- pの値=aのアドレス(=aのポインタ)
    cout << &p << endl; // 0x7ffd066ebfc8 <- pのアドレス(=pのポインタ) 
    cout << *p << endl; // 0 <- aの値

    val(a);
    cout << a << endl;  // 0
    cout << &a << endl; // 0x7ffd066ebfc4

    ref(a);
    cout << a << endl;  // 2
    cout << &a << endl; // 0x7ffd066ebfc4

    ptr(&a);
    cout << a << endl;  // 3
    cout << &a << endl; // 0x7ffd066ebfc4

    return 0;
}