Structures2 - Struct in a struct example Imagine adding...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Struct in a struct example Imagine adding these struct definitions to our former example: struct doll { char name[20]; double price; }; struct toys { struct block rubix; struct doll barbie; }; Now, consider the following functions prototypes and main: void setup_block(struct block *b); void print_block(struct block b); void setup_doll(struct doll *d); void print_doll(struct doll d); void init(struct toys *t); void print_all(struct toys t); int main() { struct toys mine; init(&mine); print_all(mine); return 0; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
// Post-condition: b will be initialized with information entered // by the user. void setup_doll(struct doll *d) { printf("Enter name,cost of the doll\n"); scanf("%s %lf", &(d->name), &(d->price)); } // Post-condition: b's components will be printed out. void print_doll(struct doll d) { printf("Name: %s, Price: %lf\n", d.name, d.price); } // Post-condition: Initializes all components of the struct t // points to. void init(struct toys *t) { setup_block(&(t->rubix)); setup_doll(&(t->barbie)); } // Post-condition: Prints out all information about t. void print_all(struct toys t) {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Structures2 - Struct in a struct example Imagine adding...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online