Advertisement
bhok

Pointers Char Version Practice

Oct 1st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. // Pointers extra review
  5.  
  6. int main()
  7. {
  8.     // A pointer is meant to "point to" an address.
  9.     // Maybe at the address there is something physical?
  10.     // We don't know, but all we know that a pointer is
  11.     // a finger that points to an address or let's say an area somewhere.
  12.  
  13.     // Here is a stack of potatoes.
  14.     // We declared it and it exists, but it has no values yet,
  15.     // but we know that this char can hold an array of 10 characters.
  16.     // char potatoes[10];
  17.  
  18.     // Here we initialize the potatoes as "red".
  19.     // In thus, potatoes are manipaulated and can be changed.
  20.     // Am I right?
  21.     char potatoes[10] = { 'r','e','d','\0' };
  22.  
  23.     // Now we declare the potato pointer.
  24.     char* potatoesPtr = potatoes;
  25.  
  26.     // If we were to print the potatoesPtr, it would give us the char array
  27.     // of potatoes[10]. Am I right?
  28.     std::cout << potatoesPtr << std::endl;
  29.    
  30.     // Now what if we added some numbers and manipulated it.
  31.     // What if we dereference it?
  32.     std::cout << potatoesPtr + 1 << std::endl;
  33.     std::cout << *(potatoesPtr + 1) << std::endl;
  34.  
  35. // fix up later
  36. int i = 5;
  37.     int p[3] = { 1,2,3 };
  38.     int* t = p;
  39.     std::cout
  40.         << t[1] << std::endl
  41.         << p[1] << std::endl
  42.         << t + 1 << std::endl
  43.         << *(t + 1);
  44.  
  45.  
  46.     system("pause");
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement