title: Segmentation fault when using if statement with pointers (BST tree)
body: <p>I'm trying to implement a binary search tree in C, more specifically looking for the predecessor. However, whenever I try to run the program I get the segmentation vault. Here's the code in question:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct tree
  {
      int a;
      tree *left;
      tree *right;
      tree *prev;
  }*root=NULL;
  
  tree *searchSpecific (tree *root, int val)
  {
      tree *x=root;
      if (!x)
      {
          return NULL;
      }
      else
      {
          while(x &amp;&amp; val!=x-&gt;a)
          {
              if (val&gt;x-&gt;a)
                  x=x-&gt;left;
              else x=x-&gt;right;
          }
      }
      return x;
  }
  
  int previous(tree *root, int f)
  {
   tree *x=searchSpecific(root,f);
      if(x-&gt;left)
      {
          x=x-&gt;left;
          while(x-&gt;right) x = x-&gt;right;
          return x-&gt;a;
      }
  
      tree *temp;
      do
      {
          temp = x;
          x = x-&gt;prev;
      } while(x &amp;&amp; (x-&gt;right != temp));
      return x-&gt;a;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The segfault appears at the if statement if(x->left) in the previous() function. I want to check if the node in question exists, but the program crashes every time and I have no idea what is wrong with it..</p>