Binary tree with structs (in C)
<p>I have a binary tree of structs. The struct is </p>

<pre><code>    typedef struct hashtag {
        char *name;
        int acc;
    } *Item;
</code></pre>

<p>The nodes are organized by the string. I want to print the node which has the highest acc but is first in alphabetical order.
My code so far:</p>

<pre><code>    Item search_max(link h) {
        int max;
        char *word;
        Item hashtag = (Item)malloc(sizeof(struct hashtag));
        Item left = (Item)malloc(sizeof(struct hashtag));
        Item right = (Item)malloc(sizeof(struct hashtag));
        hashtag = h-&gt;item;
        max = h-&gt;item-&gt;acc;
        word = h-&gt;item-&gt;name;
        if (h == NULL) 
            return 0;
        left = search_max(h-&gt;l);
        if (max == left-&gt;acc &amp;&amp; less(left-&gt;name, word))
            word = left-&gt;name;
        if (max &lt; left-&gt;acc){
            max = left-&gt;acc;
            word = left-&gt;name;
        }
        right = search_max(h-&gt;r);
        if (max == right-&gt;acc &amp;&amp; less(right-&gt;name, word))
            word = right-&gt;name;
        if (max &lt; right-&gt;acc){
            max = right-&gt;acc;
            word = right-&gt;name;
        }
        hashtag-&gt;acc = max;
        hashtag-&gt;name = word;
        return hashtag;
    }
</code></pre>

<p>h is the head of the tree and less is </p>

<pre><code>    #define less(a,b) (strcmp(a,b) &lt; 0)
</code></pre>

<p>and link is </p>

<pre><code>    typedef struct node{
        Item item;
        struct node *l;
        struct node *r;
    } *link;
</code></pre>

<p>It gives a segmentation fault(core dumped). Previously I tried the same code without allocating memory for hashtag, left or right (same error).</p>