fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //Pointers and Addresses
  8. int num = 100;
  9. int *ptr;
  10. ptr = &num; //ptr is set to the address of num
  11.  
  12. cout << ptr << endl;
  13. cout << *ptr << endl << endl; // Dereferencing
  14. cout << &num << endl;
  15. cout << num << endl << endl;
  16.  
  17. //Dynamic Variables
  18. int *ptr2;
  19. int num2 = 50;
  20. ptr2 = new int;
  21. *ptr2 = num2; // Dereference and set it equal to the value of num 2
  22. cout << num2 << endl;
  23. cout << *ptr2 << endl << endl; // Dereferencing
  24.  
  25. // Dynamic Array
  26. int *array;
  27. array = new int[4];
  28. for(int i = 0; i < 4; i++)
  29. {
  30. cout << "Put in integers: ";
  31. cin >> array[i];
  32. }
  33. for(int i = 0; i < 4; i++)
  34. {
  35. if(i==3)
  36. cout << array[i] << endl;
  37. else
  38. cout << array[i] << ", ";
  39. }
  40.  
  41. delete ptr2;
  42. delete []array;
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5264KB
stdin
Standard input is empty
stdout
0x7fff7e255cf4
100

0x7fff7e255cf4
100

50
50

Put in integers: Put in integers: Put in integers: Put in integers: 0, 0, 0, 0