1 / 12

Structures Containing Pointers Lesson xx

Structures Containing Pointers Lesson xx. Objectives. Review structures Program to demonstrate a structure containing a pointer. Structure Review. c. 2. c.pip. struct card { int pip; char suit; }; int main ( ) { card c; c.pip = 2; c.suit = ‘s’;

latham
Download Presentation

Structures Containing Pointers Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures Containing PointersLesson xx

  2. Objectives • Review structures • Program to demonstrate a structure containing a pointer

  3. Structure Review c 2 c.pip struct card { int pip; char suit; }; int main ( ) { card c; c.pip = 2; c.suit = ‘s’; return 0; } ‘s’ c.suit

  4. Structure Containing a Pointer struct xyz { int a; char b; float c; int *ptr; };

  5. Structure Containing Pointers Program #include <iostream> using std::cout; using std::endl; structstructWithPtrs{ int* p1;   double* p2; }; int main() { structWithPtrs s; int i1 = 100;   double f2;   s.p1 = &i1;   s.p2 = &f2;   *s.p2 = -45.0; cout << "i1 = " << i1 << " *s.p1 = " << *s.p1 << endl; cout << "f2 = " << f2 << " *s.p2 = " << *s.p2 << endl;   return 0; }

  6. Structure Definition structstructWithPtrs{ int* p1;   double* p2; };

  7. Variable or Object Declaration structWithPtrs s; int i1 = 100;   double f2;   s.p1 = &i1;   s.p2 = &f2;   *s.p2 = -45.0; (2fe) s s.p1 s.p2

  8. Variable Declarations structWithPtrs s; int i1 = 100;   double f2;   s.p1 = &i1;   s.p2 = &f2;   *s.p2 = -45.0; 100 i1 (1ab) (2fe) s (67b) f2 s.p1 s.p2

  9. Initializing Pointers structWithPtrs s; int i1 = 100;   double f2; s.p1 = &i1;   s.p2 = &f2;   *s.p2 = -45.0; 100 1ab 67b i1 (1ab) (2fe) s (67b) f2 s.p1 s.p2

  10. Indirection structWithPtrs s; int i1 = 100;   double f2;   s.p1 = &i1;   s.p2 = &f2; *s.p2 = -45.0; 100 1ab -45.0 67b i1 (1ab) (2fe) s (67b) f2 s.p1 s.p2

  11. Output cout << "i1 = " << i1 << " *s.p1 = " << *s.p1 << endl; cout << "f2 = " << f2 << " *s.p2 = " << *s.p2 << endl; 100 1ab -45.0 67b i1 (1ab) (2fe) s (67b) f2 s.p1 Output i1 = 100 *s.p1 = 100   f2 = -45.0 *s.p2 = -45.0 s.p2

  12. Summary • Review structures • Program to demonstrate a structure • containing a pointer

More Related