1 / 16

Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx

Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx. Objectives. Illustrate the difference between a pointer to structure and a structure that contains a pointer When to use the pointer to structure (-> ) operator When to use the dot (.) operator. Structure Setup.

kim
Download Presentation

Pointer to a Structure & Structure Containing a Pointer Difference 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. Pointer to a Structure & Structure Containing a Pointer DifferenceLesson xx

  2. Objectives • Illustrate the difference between a pointer to structure and a structure that contains a pointer • When to use the pointer to structure (-> ) operator • When to use the dot (.) operator

  3. Structure Setup struct student { long id; int age; }; int main ( ) { student s; s.id = 25691; s.age= 17; student *ps = &s; return 0; } s (2fe) 25691 s.id 17 s.age

  4. Pointer to a Structure struct student { long id; int age; }; int main ( ) { student s; s.id = 25691; s.age= 17; student *ps = &s; return 0; } 2fe ps s (2fe) 25691 s.id 17 s.age

  5. Legal Expressions with a Pointer to a Structure 2fe s.id s.age ps-> id ps->age ps s (2fe) 25691 s.id 17 s.age

  6. Illegal Expressions with a Pointer to a Structure 2fe s->id s->age ps. id ps.age ps s (2fe) 25691 s.id 17 s.age

  7. Different Syntax with Pointer to a Structure 2fe s.id (*ps).id ps->id ps s (2fe) 25691 s.id 17 s.age

  8. Incorrect Syntax with Pointer to a Structure //illegal syntax *ps.id *(ps.id) 2fe ps s (2fe) 25691 s.id //legal syntax (*ps).id 17 s.age

  9. Structure Containing Pointers struct student { long *id; int *age; }; int main ( ) { student s; long i; int a; s.id = &i; s.age= &a; *s.id = = 25691; *s.age = 17; return 0; } s.id s (55d) s.age

  10. Variable Declarations struct student { long *id; int *age; }; int main ( ) { student s; long i; int a; s.id = &i; s.age= &a; *s.id = = 25691; *s.age = 17; return 0; } s.id s (55d) s.age i (1ab) a (2fe)

  11. Initializing a Structure That Contains Pointers struct student { long *id; int *age; }; int main ( ) { student s; long i; int a; s.id = &i; s.age= &a; *s.id = = 25691; *s.age = 17; return 0; } 1ab s.id s (55d) 2fe s.age i (1ab) a (2fe)

  12. Indirection with Structure Containing Pointers struct student { long *id; int *age; }; int main ( ) { student s; long i; int a; s.id = &i; s.age= &a; *s.id = 25691; *s.age = 17; return 0; } 25691 1ab s.id 17 s (55d) 2fe s.age i (1ab) a (2fe)

  13. Legal Expressions with Structure That Contains Pointers 25691 1ab s.id 17 s (55d) *s.id= 25691; *(s.id)= 25691; (*s.id) = 25691; *s.age = 17; 2fe s.age i (1ab) a (2fe)

  14. Illegal Expressions with a Structure That Contains Pointers 25691 1ab s.id 17 s (55d) 2fe s.age (*s).id = 25691; s->id= 25691; i (1ab) a (2fe)

  15. Comparison 25691 1ab ps is a pointer to the structure s. s.id 17 s (55d) 2fe 2fe ps s.age i (1ab) s (2fe) a (2fe) 25691 s.id 17 s.age s is a structure that contains 2 pointers

  16. Summary • Illustrate the difference between a pointer to structure and a structure that contains a pointer • When to use the pointer to structure (-> ) operator • When to use the dot (.) operator

More Related