Review structures Program to demonstrate a structure containing a pointer
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
struct xyz { int a; char b; float c; int *ptr; };
#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; }
struct structWithPtrs { int* p1; double* p2; };
structWithPtrs s; int i1 = 100; double f2; s.p1 = &i1; s.p2 = &f2; *s.p2 = -45.0; s.p1 s.p2 (2fe) s
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)
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)
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 f2 i1(1ab) (67b)
cout << "i1 = " << i1 << " *s.p1 = " << *s.p1 << endl; cout << "f2 = " << f2 << " *s.p2 = " << *s.p2 << endl; 1ab 67b s.p1 s.p2 (2fe) s f2 i1(1ab) (67b) Output i1 = 100 *s.p1 = 100 f2 = *s.p2 = -45.0
Review structures Program to demonstrate a structure containing a pointer