fork download
  1. #include <iostream>
  2.  
  3. int value1 = 1;
  4. int value2 = 2;
  5.  
  6. void func1(int *p) {
  7. p = &value2;
  8. }
  9.  
  10. void func2(int **p) {
  11. *p = &value2;
  12. }
  13.  
  14. int main(void) {
  15. std::cout << "The address of value1 is " << &value1 << std::endl;
  16. std::cout << "The address of value2 is " << &value2 << std::endl << std::endl;
  17.  
  18. std::cout << "========== Case 1 ==========" << std::endl;
  19. int *ptr1 = &value1;
  20. int *temp1 = ptr1;
  21. std::cout << "\tptr1 = " << ptr1 << std::endl;
  22. func1(temp1);
  23. std::cout << "\tptr1 = " << ptr1 << std::endl << std::endl;
  24.  
  25. std::cout << "========== Case 2 ==========" << std::endl;
  26. int *ptr2 = &value1;
  27. int **temp2 = &ptr2;
  28. std::cout << "\tptr2 = " << ptr2 << std::endl;
  29. func2(temp2);
  30. std::cout << "\tptr2 = " << ptr2 << std::endl;
  31. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
The address of value1 is 0x55ac40460014
The address of value2 is 0x55ac40460010

========== Case 1 ==========
	ptr1 = 0x55ac40460014
	ptr1 = 0x55ac40460014

========== Case 2 ==========
	ptr2 = 0x55ac40460014
	ptr2 = 0x55ac40460010