fork download
  1. #include <iostream>
  2.  
  3. void call_by_value(int x, int *y) {
  4. *y = 2; // 呼出し元に影響する
  5. x = 3; // 呼出し元に影響しない
  6. static int z = 4;
  7. y = &z; // 呼出し元に影響しない
  8. }
  9.  
  10. void call_by_reference(int &x, int *&y) {
  11. *y = 5; // 呼出し元に影響する
  12. x = 6; // 呼出し元に影響する
  13. static int z = 7;
  14. y = &z; // 呼出し元に影響する
  15. }
  16.  
  17. int main(void) {
  18. int value = 1; // 値型変数
  19. int *pointer = &value; // ポインタ型変数
  20. call_by_value(value, pointer); // 値型変数、ポインタ型変数の値渡し
  21. std::cout << value << " " << *pointer << std::endl; // 2 2
  22. call_by_reference(value, pointer); // 値型変数、ポインタ型変数の参照渡し
  23. std::cout << value << " " << *pointer << std::endl; // 6 7
  24. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2 2
6 7