title: Printing Structs inside Structs
body: <p>I want to print a structure of structures. My code currently looks like this: (I haven't pasted it here, but Shelf is just a typedef of struct shelf).</p>
  <pre><code>struct shelf {
      struct book *books;
      struct shelf *next;
  };
  
  struct book {
      int text;
      int image;
      struct book *next;
  };
  
  Shelf create_shelf(void) {
      Shelf new_shelf = malloc(sizeof (struct shelf));
      new_shelf-&gt;next = NULL;
      new_shelf-&gt;books = NULL;
      return new_shelf;
  }
  
  </code></pre>
  <p>I now want to print my shelves, the books inside them and each image and text in each of these books like this:</p>
  <p>Output: , , ... and so on, where text1 and image1 refers to book1.</p>
  <p>I've started to try to code this, but I cannot understand what is wrong with my print function below. How would I approach printing everything <strong>while only allowing the input &quot;Shelf shelf&quot; as the argument in my function?</strong></p>
  <pre><code>void print_everything (Shelf shelf) {
      while (shelf != NULL) {
          printf(&quot;%d, %d&quot;, shelf-&gt;books-&gt;text, shelf-&gt;books-&gt;image);
      }
  }
  </code></pre>
  <p>Thanks!</p>