addpntrs.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. // addpntrs.cpp -- pointer addition
  2. #include <iostream>
  3. int main()
  4. {
  5. using namespace std;
  6. double wages[3] = {10000.0, 20000.0, 30000.0};
  7. short stacks[3] = {3, 2, 1};
  8. // Here are two ways to get the address of an array
  9. double * pw = wages; // name of an array = address
  10. short * ps = &stacks[0]; // or use address operator
  11. // with array element
  12. cout << "pw = " << pw << ", *pw = " << *pw << endl;
  13. pw = pw + 1;
  14. cout << "add 1 to the pw pointer:\n";
  15. cout << "pw = " << pw << ", *pw = " << *pw << "\n\n";
  16. cout << "ps = " << ps << ", *ps = " << *ps << endl;
  17. ps = ps + 1;
  18. cout << "add 1 to the ps pointer:\n";
  19. cout << "ps = " << ps << ", *ps = " << *ps << "\n\n";
  20. cout << "access two elements with array notation\n";
  21. cout << "stacks[0] = " << stacks[0]
  22. << ", stacks[1] = " << stacks[1] << endl;
  23. cout << "access two elements with pointer notation\n";
  24. cout << "*stacks = " << *stacks
  25. << ", *(stacks + 1) = " << *(stacks + 1) << endl;
  26. cout << sizeof(wages) << " = size of wages array\n";
  27. cout << sizeof(pw) << " = size of pw pointer\n";
  28. // cin.get();
  29. return 0;
  30. }