Skip to content

Instantly share code, notes, and snippets.

@mu-777
Last active January 10, 2016 02:59
Show Gist options
  • Save mu-777/5840bff9ef3ebb4a9f29 to your computer and use it in GitHub Desktop.
Save mu-777/5840bff9ef3ebb4a9f29 to your computer and use it in GitHub Desktop.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment