Advertisement
GregLeck

Pointers and Structs

Feb 15th, 2022
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include<iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Container
  6. {
  7.     string Name;
  8.     int X;
  9.     int Y;
  10.     int Z;
  11. };
  12.  
  13. int main()
  14. {
  15.     Container container = { "Sam", 30, 20, 50 };
  16.  
  17.     // Create pointer to 'container'
  18.     Container* PtrToCont = &container;
  19.  
  20.     // Dereference PtrToCon --- should print 'Sam' in both instances
  21.     // (Parentheses required to dereference 'PtrToCont' first
  22.     // before using dot operator)
  23.     cout << (*PtrToCont).Name << endl;
  24.     // OR, use this preferred syntax:
  25.     cout << PtrToCont->Name << endl;
  26.  
  27.     system("pause");
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement