title: Do we have to malloc a struct?
body: <p>Consider the following code:</p>
  
  <pre><code>struct Node {
      void* data;
      int ref;
      struct Node* next;
  };
  typedef struct Node* NodePtr;
  </code></pre>
  
  <p>I have found that I am getting segfaults whenever I try to do anything with NodePtr's fields. E.g.:</p>
  
  <pre><code>NodePtr node;
  node-&gt;ref = 1;
  </code></pre>
  
  <p>So I allocated some space for the NodePtr, and now it seems to work fine.  Why is this?  My guess is that since node is just a pointer, it has no memory for its fields.</p>
  
  <p>So I tried to initialize the NodePtr:</p>
  
  <pre><code>NodePtr node = {
      node-&gt;data = 0;
      node-&gt;next = NULL;
      node-&gt;ref = 0;
  };
  </code></pre>
  
  <p>And well, I got this error: </p>
  
  <pre><code>error: expected â}â before â;â token
  </code></pre>
  
  <p>This boils down to four questions:</p>
  
  <ol>
  <li>If my guess is incorrect, why doesn't it work if I don't use malloc()?</li>
  <li>Why does my initialization not work?</li>
  <li>Would initializing a struct offer memory on the stack and solve my problem?</li>
  <li>If not, do I have an alternative to having to allocate memory for every struct I use?</li>
  </ol>