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

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

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; }
Image of page 1

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

Here are the added functions: // 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '08
  • Guha
  • Return statement, toy, Hotels, Void type, struct

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern