Download presentation
Presentation is loading. Please wait.
Published byWillis Mills Modified over 9 years ago
3
Review structures Program to demonstrate a structure containing a pointer
4
struct card { int pip; char suit; }; int main ( ) { card c; c.pip = 2; c.suit = ‘s’; return 0; } 2 ‘s’ c.pip c.suit c
5
struct xyz { int a; char b; float c; int *ptr; };
6
#include using std::cout; using std::endl; struct structWithPtrs { 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; }
7
struct structWithPtrs { int* p1; double* p2; };
8
structWithPtrs s; int i1 = 100; double f2; s.p1 = &i1; s.p2 = &f2; *s.p2 = -45.0; s.p1 s.p2 (2fe) s
9
structWithPtrs s; int i1 = 100; double f2; s.p1 = &i1; s.p2 = &f2; *s.p2 = -45.0; s.p1 s.p2 (2fe) s 100 f2 i1(1ab) (67b)
10
structWithPtrs s; int i1 = 100; double f2; s.p1 = &i1; s.p2 = &f2; *s.p2 = -45.0; 1ab 67b s.p1 s.p2 (2fe) s 100 f2 i1(1ab) (67b)
11
structWithPtrs s; int i1 = 100; double f2; s.p1 = &i1; s.p2 = &f2; *s.p2 = -45.0; 1ab 67b s.p1 s.p2 (2fe) s 100 -45.0 f2 i1(1ab) (67b)
12
cout << "i1 = " << i1 << " *s.p1 = " << *s.p1 << endl; cout << "f2 = " << f2 << " *s.p2 = " << *s.p2 << endl; 1ab 67b s.p1 s.p2 (2fe) s 100 -45.0 f2 i1(1ab) (67b) Output i1 = 100 *s.p1 = 100 f2 = -45.0 *s.p2 = -45.0
13
Review structures Program to demonstrate a structure containing a pointer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.