380 likes | 521 Views
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 ;
E N D
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
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
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
Access the members (or attributes)! ecs30b Fall 2008 Lecture #21
#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
¤t_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
¤t_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
#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
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
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
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(¤t); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #21
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
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(¤t); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #24
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
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
Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); ecs30b Fall 2008 Lecture #24
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
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
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
#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
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
#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
struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24
struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24
struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet p[12]; }; ecs30b Fall 2008 Lecture #24
printf("%d\n", ((planet_t *) 0)->diameter); ecs30b Fall 2008 Lecture #24
#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
#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
#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
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
I386 architecture typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; ecs30b Fall 2008 Lecture #24
{"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