title: Different behaviour for malloc(sizeof(struct Node)) vs malloc(sizeof(nodeptr))
body: <p>I was trying to allocate memory using malloc, I am not able to understand why I am getting a different result for these two malloc calls.</p>
  
  <blockquote>
    <p>The line below gives me wrong result even though with gdb I see the
    data is getting the correct value assigned.</p>
  </blockquote>
  
  <ul>
  <li><p>nodeptr n = malloc(sizeof(nodeptr));</p>
  
  <p>Value head->data: '!'<br>
  Value head->eq->data: ''</p></li>
  </ul>
  
  <blockquote>
    <p>And when I do this get the correct result:  </p>
  </blockquote>
  
  <ul>
  <li><p>nodeptr n = malloc(sizeof(struct Node));</p>
  
  <p>Value head->data: 'w'<br>
  Value head->eq->data: 'X'</p></li>
  </ul>
  
  <p>I followed <a href="https://stackoverflow.com/questions/29487252/whats-the-correct-way-to-malloc-struct-pointer-using-sizeof">this</a> post, I think I am doing it correctly.<br>
  <strong>In both ways, while allocation I get the same amount of memory, just I see the different results in the end.</strong></p>
  
  <pre><code>typedef struct Node
  {
      struct Node *left, *right, *eq;
      char data;
      bool isEnd;
  } *nodeptr;
  
  nodeptr newNode(const char c) {
      nodeptr n = malloc(sizeof(nodeptr));
      // nodeptr n = malloc(sizeof(struct Node));
      n-&gt;data = c;
      n-&gt;left = NULL;
      n-&gt;right = NULL;
      n-&gt;left = NULL;
      n-&gt;isEnd = false;
  
      return n;
  }
  
  void insert(nodeptr *node, const char *str) {
  
      if (*node == NULL) {
          *node = newNode(*str);
      }
      nodeptr pCrawl = *node;
  
      if(pCrawl-&gt;data &lt; *str) {
          insert(&amp;pCrawl-&gt;right, str);
      } else if (pCrawl-&gt;data &gt; *str) {
          insert(&amp;pCrawl-&gt;left, str);
      } else {
          if(*(str+1)) {
              insert(&amp;pCrawl-&gt;eq, str + 1);
          } else {
              pCrawl-&gt;isEnd = true;
          }
      }
  }
  
  int main(int argc, char const *argv[])
  {
  
      const char* const strs[5]= {
          "w.",
      };
      nodeptr head = NULL;
      for(int i = 0; i&lt;1; i++) {
          insert(&amp;head, strs[i]);
      }
      return 0;
      printf("Value head-&gt;data: \'%c\'\n", head-&gt;data);
      printf("Value head-&gt;eq-&gt;data: \'%c\'\n", head-&gt;eq-&gt;data);
  }
  </code></pre>