1 / 38

Prof . S. Felix Wu Computer Science Department University of California, Davis

ecs30 Winter 2012: Programming and Problem Solving # 19: Chapters 11~12, Structure , File + JSON. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. struct compound data type. #define STRSIZ 10 t ypedef int Felix_integer_t ;

drea
Download Presentation

Prof . S. Felix Wu Computer Science Department University of California, Davis

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. ecs30 Winter 2012:Programming and Problem Solving#19: Chapters 11~12, Structure, File + JSON Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #17

  2. structcompound data type #define STRSIZ 10 typedefintFelix_integer_t; typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; }planet_t; … planet_tcurrent_planet; ecs30b Fall 2008 Lecture #21

  3. ecs30 Winter 2012 Lecture #17

  4. structcompound data type #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; }; … struct planet current_planet; ecs30b Fall 2008 Lecture #24

  5. Access the members (or attributes)! ecs30b Fall 2008 Lecture #21

  6. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet; ecs30b Fall 2008 Lecture #21

  7. &current_planet #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet; ecs30b Fall 2008 Lecture #21

  8. &current_planet #define STRSIZ 10 typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_tcurrent_planet; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #21

  9. #include <stdio.h> #include <stdlib.h> #define STRSIZ 10 typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; intmain(void) { printf("sizeof = %d\n", sizeof(planet_t)); return 0; } sizeof = 38 ecs30 Winter 2012 Lecture #17

  10. planet_t xyz(planet_t x) { strcpy(x.name, "abcdefghj"); return x; } int main(void) { planet_t current; planet_t next; strcpy(current.name, "rstuvwxyz"); next = xyz(current); printf("%s\n", next.name); return 0; } ecs30b Fall 2008 Lecture #21

  11. ecs30b Fall 2008 Lecture #21

  12. planet_t xyz(planet_t x) { strcpy(x.name, "abcdefghj"); return x; } int main(void) { planet_t current; scanf(“%d”, &(current.moons)); return 0; } ecs30b Fall 2008 Lecture #21

  13. planet_t * xyz_cbr(planet_t *xp) { strcpy(xp->name, "abcdefghj"); return xp; } int main(void) { planet_t current; planet_t *next; strcpy(current.name, "rstuvwxyz"); next = xyz_cbr(&current); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #21

  14. struct X vs. struct X * • Access to the individual attribute(s): • struct X x; ~ x.<attrID> • struct X *xp; ~ xp-><attrID> • xp = &x; • x = *xp; • y = x; ecs30b Fall 2008 Lecture #24

  15. ecs30b Fall 2008 Lecture #24

  16. planet_t * xyz_cbr(planet_t *xp) { strcpy(xp->name, "abcdefghj"); return xp; } int main(void) { planet_t current; planet_t *next; strcpy(current.name, "rstuvwxyz"); next = xyz_cbr(&current); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #24

  17. Assignment for struct planet_t x,y; y = x; strcpy(y.name, x.name); y.diameter = x.diameter; y.moons = x.moons; y.orbit_time = x.orbit_time; y.rotation_time = x.rotation_time; ecs30b Fall 2008 Lecture #24

  18. Assignment for struct planet_t x,y; y = x; strcpy(y.name, x.name); y.diameter = x.diameter; y.moons = x.moons; y.orbit_time = x.orbit_time; y.rotation_time = x.rotation_time; Not entirely right!! ecs30b Fall 2008 Lecture #24

  19. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); ecs30b Fall 2008 Lecture #24

  20. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); strncpy(&y, &x, sizeof(planet_t)); ecs30b Fall 2008 Lecture #24

  21. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); strncpy(&y, &x, sizeof(planet_t)); The strncpy() function copies at most n characters from &x into &y. If &x is less than n characters long, the remainder of &y is filled with ‘\0’ characters. Otherwise, &y is not terminated. ecs30b Fall 2008 Lecture #24

  22. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); planet_t myfunc(…); x = myfunc(…); ecs30b Fall 2008 Lecture #24

  23. #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet next; struct planet previous; }; ecs30b Fall 2008 Lecture #24

  24. No Recursive in struct! #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet next; struct planet previous; }; sizeof(struct planet) == ? ecs30b Fall 2008 Lecture #24

  25. #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet *next; struct planet *previous; }; sizeof(struct planet) == ? ecs30b Fall 2008 Lecture #24

  26. struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24

  27. struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24

  28. struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet p[12]; }; ecs30b Fall 2008 Lecture #24

  29. printf("%d\n", ((planet_t *) 0)->diameter); ecs30b Fall 2008 Lecture #24

  30. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", ((planet_t *) 0)->diameter); printf("%d\n", &(((planet_t *) 0)->diameter)); ecs30b Fall 2008 Lecture #24

  31. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", (int) &(((planet_t *) 0)->name)); printf("%d\n", (int) &(((planet_t *) 0)->diameter)); printf("%d\n", (int) &(((planet_t *) 0)->moons)); ecs30b Fall 2008 Lecture #24

  32. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", (int) &(((planet_t *) 0)->name)); printf("%d\n", (int) &(((planet_t *) 0)->diameter)); printf("%d\n", (int) &(((planet_t *) 0)->moons)); Expect: 0, 10, 18 MacBook/pc16 (i386): 0, 12, 20 pc26 (x86_64): 0, 16, 24 ecs30b Fall 2008 Lecture #24

  33. typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; &((planet_t *) 0)->diameter) “Be very careful! It’s Machine-Dependent actually!!” ecs30b Fall 2008 Lecture #24

  34. I386 architecture typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; ecs30b Fall 2008 Lecture #24

  35. ecs30 Winter 2012 Lecture #17

  36. {"id":"204722549606084_262650593813279","from":{"name":"Corey Hobbs","id":"100001896707606"},"to\ ":{"data":[{"version":1,"name":"ecs30 Programming and Problem Solving","id":"204722549606084"}]}\ ,"message":"Just some ECS30 humor to lighten your Day! :)","picture":"http:\/\/photos-a.ak.fbcdn\ .net\/hphotos-ak-snc7\/429242_281418948597993_100001896707606_668040_1257901773_s.jpg","link":"h\ ttp:\/\/www.facebook.com\/photo.php?fbid=281418948597993&set=o.204722549606084&type=1","icon":"h\ ttp:\/\/static.ak.fbcdn.net\/rsrc.php\/v1\/yz\/r\/StEh3RhPvjk.gif","actions":[{"name":"Comment",\ "link":"http:\/\/www.facebook.com\/204722549606084\/posts\/262650593813279"},{"name":"Like","lin\ k":"http:\/\/www.facebook.com\/204722549606084\/posts\/262650593813279"}],"type":"photo","object\ _id":"281418948597993","created_time":"2012-03-02T16:37:27+0000","updated_time":"2012-03-02T16:4\ 5:18+0000","likes":{"data":[{"name":"Peter D. Lai","id":"633444184"}],"count":2},"comments":{"da\ ta":[{"id":"204722549606084_262650593813279_262652393813099","from":{"name":"Matt Song","id":"76\ 7960581"},"message":"Already made a meme :D\nhttps:\/\/fbcdn-sphotos-a.akamaihd.net\/hphotos-ak-\ snc7\/s720x720\/418154_10151339744545582_767960581_22979633_746789299_n.jpg","created_time":"201\ 2-03-02T16:41:17+0000","likes":2},{"id":"204722549606084_262650593813279_262654177146254","from"\ :{"name":"Corey Hobbs","id":"100001896707606"},"message":"-___-","created_time":"2012-03-02T16:4\ 5:15+0000"},{"id":"204722549606084_262650593813279_262654200479585","from":{"name":"Corey Hobbs"\ ,"id":"100001896707606"},"message":"i saw... lol","created_time":"2012-03-02T16:45:18+0000"}],"c\ ount":3},"is_published":true} ecs30 Winter 2012 Lecture #17

  37. ecs30 Winter 2012 Lecture #17

  38. ecs30 Winter 2012 Lecture #17

More Related