SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Pointers to Structures and Structures Containing Pointers in C

kim avatar
16 diapositives · 323 Views · Aug 25, 2014

This lesson highlights the critical differences between a pointer to a structure and a structure that contains a pointer. It covers the appropriate usage of the pointer-to-structure operator (->) and the dot operator (.) for accessing structure members. Example code demonstrates how to declare structures with pointers, initialize them, and legally access their members using both pointer and structure syntax. Key legal and illegal expressions are reviewed to enhance understanding of syntax and proper coding practices in C programming.

Understanding Pointers to Structures and Structures Containing Pointers in C - slide 1
01 / 16Understanding Pointers to Structures and Structures Containing Pointers in C
1 / 17

Télécharger la présentation

Understanding Pointers to Structures and Structures Containing Pointers in C
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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