pointers iii struct pointers
play

Pointers III: Struct & Pointers 1 Structures What - PowerPoint PPT Presentation

Pointers III: Struct & Pointers 1 Structures What is a structure? One or more values, called members, with possibly dissimilar types that are


  1. Pointers ¡III: ¡Struct ¡& ¡Pointers ¡ 1 ¡

  2. Structures ¡ • What ¡is ¡a ¡structure? ¡ – One ¡or ¡more ¡values, ¡called ¡members, ¡with ¡possibly ¡dissimilar ¡ types ¡that ¡are ¡stored ¡together. ¡ – Used ¡to ¡group ¡together ¡different ¡types ¡of ¡variables ¡under ¡the ¡ same ¡name. ¡ – Aggregates ¡a ¡fixed ¡set ¡of ¡labeled ¡objects, ¡possibly ¡of ¡different ¡ types, ¡into ¡a ¡single ¡object ¡(like ¡a ¡record) ¡ • What ¡is ¡a ¡structure ¡NOT? ¡ – Since ¡members ¡are ¡NOT ¡the ¡same ¡type/size, ¡they ¡are ¡not ¡as ¡ easy ¡to ¡access ¡as ¡array ¡elements ¡that ¡are ¡the ¡same ¡size. ¡ – Structure ¡variable ¡names ¡are ¡NOT ¡replaced ¡with ¡a ¡pointer ¡in ¡an ¡ expression ¡(like ¡arrays) ¡ – A ¡structure ¡is ¡NOT ¡an ¡array ¡of ¡its ¡members ¡so ¡can ¡NOT ¡use ¡ subscripts. ¡ 2 ¡

  3. Structure ¡Example ¡(preview) ¡ This ¡declaraLon ¡introduces ¡the ¡type ¡struct ¡fracLon ¡(both ¡words ¡are ¡required) ¡as ¡a ¡ ¡ • new ¡type. ¡ ¡C ¡uses ¡the ¡period ¡(.) ¡to ¡access ¡the ¡fields ¡in ¡a ¡record. ¡ ¡ • You ¡can ¡copy ¡two ¡records ¡of ¡the ¡same ¡type ¡using ¡a ¡single ¡assignment ¡statement, ¡ • however ¡== ¡does ¡not ¡work ¡on ¡structs ¡(see ¡note ¡link). ¡ struct ¡frac5on ¡{ ¡ ¡ ¡ ¡ ¡int ¡numerator; ¡ ¡ ¡ ¡ ¡int ¡denominator; ¡// ¡can’t ¡ini5alize ¡ }; ¡ ¡ ¡ struct ¡frac5on ¡f1, ¡f2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡declare ¡two ¡frac5ons ¡ f1.numerator ¡= ¡25; ¡ f1.denominator ¡= ¡10; ¡ f2 ¡= ¡f1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡this ¡copies ¡over ¡the ¡whole ¡struct ¡ 3 ¡

  4. Structure ¡Declara5ons ¡ struct ¡ ¡tag ¡ ¡ ¡{member_list} ¡ ¡variable_list; ¡ struct ¡S ¡{ ¡ struct ¡ ¡{ ¡ struct ¡S ¡{ ¡ ¡ ¡ ¡ ¡int ¡a; ¡ int ¡a; ¡ int ¡a; ¡ ¡ ¡ ¡ ¡float ¡b; ¡ float ¡b; ¡ float ¡b; ¡ struct ¡S ¡y; ¡ } ¡x; ¡ } ¡z; ¡ }; ¡ OmiUng ¡the ¡ struct ¡S; ¡ Declares ¡x ¡to ¡ member ¡list ¡ OmiUng ¡the ¡ OmiUng ¡the ¡ be ¡a ¡structure ¡ Incomplete ¡ tag ¡field; ¡ declares ¡ variable ¡list ¡ declaraLon ¡ having ¡two ¡ another ¡ cannot ¡ defines ¡the ¡ members, ¡a ¡ which ¡informs ¡ create ¡ ¡any ¡ structure ¡ tag ¡S ¡for ¡use ¡ the ¡compiler ¡ and ¡b. ¡In ¡ variable ¡y ¡ more ¡ in ¡later ¡ addiLon, ¡the ¡ that ¡S ¡is ¡a ¡ variables ¡ with ¡the ¡ declaraLons ¡ structure ¡tag ¡ structure ¡tag ¡S ¡ with ¡the ¡ same ¡type ¡ to ¡be ¡defined ¡ is ¡created ¡for ¡ as ¡x ¡ same ¡type ¡ use ¡in ¡future ¡ later ¡ as ¡z ¡ declaraLons. ¡ 4 ¡

  5. Structure ¡Declara5ons ¡(cont) ¡ • So ¡tag, ¡member_list ¡and ¡variable_list ¡are ¡all ¡opLonal, ¡ but ¡cannot ¡all ¡be ¡omiXed; ¡at ¡least ¡two ¡must ¡appear ¡for ¡ a ¡complete ¡declaraLon. ¡ Single ¡variable ¡x ¡contains ¡3 ¡members ¡ struct ¡ ¡{ ¡ ¡ ¡ ¡ ¡int ¡a; ¡ ¡ ¡ ¡ ¡char ¡b; ¡ Structs ¡on ¡the ¡leP ¡are ¡treated ¡different ¡ ¡ ¡ ¡ ¡float ¡c; ¡ by ¡the ¡compiler ¡ } ¡x; ¡ DIFFERENT ¡TYPES ¡ i.e. ¡z ¡= ¡&x ¡is ¡ILLEGAL ¡ struct ¡ ¡{ ¡ ¡ ¡ ¡ ¡int ¡a; ¡ ¡ ¡ ¡ ¡char ¡b; ¡ An ¡array ¡of ¡20 ¡structures ¡(y); ¡and ¡ ¡ ¡ ¡ ¡float ¡c; ¡ A ¡pointer ¡to ¡a ¡structure ¡of ¡this ¡type ¡(z) ¡ } ¡ ¡y[20], ¡*z; ¡ 5 ¡

  6. More ¡Structure ¡Declara5ons ¡ • The ¡TAG ¡field ¡ – Allows ¡a ¡name ¡to ¡be ¡given ¡to ¡the ¡member ¡list ¡so ¡that ¡it ¡can ¡be ¡ referenced ¡in ¡subsequent ¡declaraLons ¡ – Allows ¡many ¡declaraLons ¡to ¡use ¡the ¡same ¡member ¡list ¡and ¡thus ¡ create ¡structures ¡of ¡the ¡same ¡type ¡ struct ¡ ¡SIMPLE ¡{ ¡ ¡ ¡ ¡ ¡int ¡a; ¡ So ¡ è è ¡ ¡struct ¡SIMPLE ¡ ¡x; ¡ ¡ ¡ ¡ ¡char ¡b; ¡ ¡struct ¡SIMPLE ¡y[20], ¡*z; ¡ ¡ ¡ ¡ ¡float ¡c; ¡ ¡ Now ¡x, ¡y, ¡and ¡z ¡are ¡all ¡the ¡same ¡ } ¡; ¡ kind ¡of ¡structure ¡ Associates ¡tag ¡with ¡ member ¡list; ¡does ¡not ¡ create ¡any ¡variables ¡ 6 ¡

  7. Typedefs è è typedef <type> <name>; • Ex1: ¡ • #define ¡true ¡1 ¡ • #define ¡false ¡0 ¡ • typedef ¡ ¡ ¡int ¡ ¡bool; ¡ • bool ¡ ¡ ¡flag ¡= ¡false; ¡ • Ex2: ¡ • char ¡ ¡ ¡*ptr_to_char; ¡ ¡// ¡new ¡variable ¡ • typedef ¡ ¡ ¡char ¡* ¡ ¡ptr_to_char; ¡// ¡new ¡type ¡ • ptr_to_char ¡a; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡new ¡variable ¡ 7 ¡

  8. Using ¡typedefs ¡with ¡Structures ¡ • A ¡typedef ¡statement ¡introduces ¡a ¡shorthand ¡name ¡for ¡ a ¡type. ¡The ¡syntax ¡is... ¡ – typedef ¡<type> ¡<name>; ¡ • shorter ¡to ¡write ¡ • can ¡simplify ¡more ¡complex ¡type ¡definiLons ¡ So ¡ è è ¡ ¡Simple ¡ ¡x; ¡ typedef ¡struct ¡ ¡{ ¡ ¡Simple ¡y[20], ¡*z; ¡ ¡ ¡ ¡ ¡int ¡a; ¡ ¡ Now ¡x, ¡y, ¡and ¡z ¡are ¡all ¡the ¡same ¡ ¡ ¡ ¡ ¡char ¡b; ¡ ¡ ¡ ¡ ¡float ¡c; ¡ TYPE. ¡ } ¡Simple; ¡ ¡ Similar ¡to ¡ è è ¡ ¡ ¡int ¡x; ¡ ¡ ¡int ¡y[20], ¡*z; ¡ 8 ¡

  9. Typedef ¡Structure ¡Example ¡ #include ¡<stdio.h> ¡ ¡ typedef ¡struct ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡x; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡y; ¡ ¡ } ¡point; ¡ ¡ int ¡main(void) ¡ ¡ { ¡/* ¡Define ¡a ¡variable ¡p ¡of ¡type ¡point, ¡and ¡ini5alize ¡all ¡its ¡members ¡inline! ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡point ¡p ¡= ¡{1,2}; ¡ ¡ ¡ ¡ ¡ ¡ ¡point ¡q; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡q ¡= ¡p; ¡ ¡// ¡q.x ¡= ¡1 ¡and ¡q.y=2 ¡ ¡ ¡ ¡ ¡ ¡ ¡q.x ¡= ¡2; ¡ ¡ /* ¡Demonstrate ¡we ¡have ¡a ¡copy ¡and ¡that ¡they ¡are ¡now ¡different. ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(p.x ¡!= ¡q.x) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prinf("The ¡members ¡are ¡not ¡equal! ¡%d ¡!= ¡%d", ¡p.x, ¡q.x); ¡ ¡ return ¡0; ¡} ¡ 9 ¡

  10. Structures ¡and ¡Pointers ¡ #include<stdio.h> ¡ What ¡is ¡going ¡on ¡ ¡ here? ¡ ¡ typedef ¡struct ¡ ¡ ¡ ¡ Remember: ¡ { ¡ ¡ ¡ ¡char ¡ ¡*name; ¡ TELEPHONE ¡is ¡a ¡ type ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡number; ¡ of ¡structure; ¡ } ¡TELEPHONE; ¡ ¡ ¡ ¡ int ¡main() ¡ ¡ { ¡ ¡TELEPHONE ¡ ¡index; ¡ ¡ ¡ ¡ ¡ ¡TELEPHONE ¡*ptr_myindex; ¡ ¡ ¡ ¡ ¡ ¡ptr_myindex ¡= ¡&index; ¡ ¡ ¡ ¡ ¡ ¡ptr_myindex-­‑>name ¡= ¡"Jane ¡Doe"; ¡ ¡ ¡ ¡ ¡ ¡ptr_myindex-­‑>number ¡= ¡12345; ¡ ¡ ¡ ¡ ¡ ¡prinf("Name: ¡%s\n", ¡ptr_myindex-­‑>name); ¡ ¡ ¡ ¡ ¡ ¡prinf("Telephone ¡number: ¡%d\n", ¡ptr_myindex-­‑>number); ¡ ¡ ¡return ¡0; ¡} ¡ ¡ 10 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend