fork download
  1. /*-----------------------
  2.  
  3. ポインタ説明
  4.  
  5. ポインタの説明するのに、値渡し/参照渡しアプローチと
  6. 配列のダイレクト操作アプローチしてる人が居る感
  7.  
  8. -----------------------*/
  9.  
  10. #include <iostream>
  11. using namespace std;
  12.  
  13. // 値渡し/参照渡しアプローチ
  14. void add1_A(int foo){ cout << &foo << endl; foo++; }
  15. void add1_B(int *foo){ cout << foo << endl; (*foo)++; }
  16. void f01(){
  17. cout<<"値渡し/参照渡しアプローチ"<<endl;
  18. int a = 10;
  19. cout << &a << endl;
  20. add1_A(a); //aが11になってくれない
  21. add1_B(&a); //aが11になってくれる
  22. }
  23.  
  24. //配列アプローチ
  25. void f02(){
  26. cout<<"配列アプローチ"<<endl;
  27. int a[10] = {0,1,2,3,4,5,6,7,8,9};
  28. cout<<a[0]<<endl;
  29. cout<<a<<endl;
  30. cout<<(*a)<<endl;
  31.  
  32. cout<<a[1]<<endl;
  33. cout<<a+1<<endl;
  34. cout<<(*a+1)<<endl;
  35.  
  36. }
  37.  
  38. //mallocアプローチ
  39. void f03(){
  40. cout<<"mallocアプローチ"<<endl;
  41. void *a = malloc(4);
  42. cout << a << endl;
  43. int *b = (int*)a;
  44. cout << b << endl;
  45. (*b) = 10;
  46. cout << (*b) << endl;
  47. free(a);
  48. }
  49.  
  50. int main() {
  51. f01();
  52. f02();
  53. f03();
  54. return 0;
  55. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
値渡し/参照渡しアプローチ
0xbf9c8d0c
0xbf9c8cf0
0xbf9c8d0c
配列アプローチ
0
0xbf9c8ce8
0
1
0xbf9c8cec
1
mallocアプローチ
0x9160008
0x9160008
10