title: Segmentation Fault for Expression Tree implementation in C
body: <p>C noob here,</p>
  <p>I`'m trying to implement a C program to convert a POSTFIX expression into
  a valid PREFIX expression by using an expression tree.</p>
  <p>I created 2 files, a <em>TreeNode.h</em> file and a <em>main.c</em> file</p>
  <p>TreeNode.h:</p>
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;malloc.h&gt;
  
  typedef char TreeEntry;
  
  typedef struct treenode
  {
      char entry;
      struct treenode * left;
      struct treenode * right;
  }*TreeNode;
  
  
  bool isOperator(char c)
  {
      if (c == '+' || c == '-' ||
          c == '*' || c == '/' ||
          c == '^'){
              return true;
          }
      return false;
  }
  
  
  TreeNode newNode(TreeEntry v){
      TreeNode temp;
      temp-&gt;left = temp-&gt;right = 0;
      temp-&gt;entry = v;
      return temp;
  }
  
  void inOrder(TreeNode n){
      if(n){
          inOrder(n-&gt;left);
          printf(&quot;%s&quot;,n-&gt;entry);
          inOrder(n-&gt;right);
      }
  }
  
  void preOrder(TreeNode n){
      if(n){
          printf(&quot;%s&quot;,n-&gt;entry);
          preOrder(n-&gt;left);
          preOrder(n-&gt;right);
      }
  }
  
  void postOrder(TreeNode n){
      if(n){
          postOrder(n-&gt;left);
          postOrder(n-&gt;right);
          printf(&quot;%s&quot;,n-&gt;entry);
      }
  }
  
  
  </code></pre>
  <p>main.c:</p>
  <pre><code>
  #include &lt;stdio.h&gt;
  #include &quot;TreeNode.h&quot;
  
  #define MAXSTACK 50
  
  TreeNode stack[MAXSTACK];
  int top = -1;
  
  void push(TreeNode n){
      stack[++top] = n;
  }
  
  TreeNode pop(){
      TreeNode n = stack[top--];
      return n;
  }
  
  TreeNode createExpTree(char postfix[]){
      TreeNode temp,et;
      for (int i = 0; i &lt; strlen(postfix); i++){
          if(!isOperator(postfix[i])){
              temp = newNode(postfix[i]);
              push(temp);
          }else{
              temp = newNode(postfix[i]);
              temp-&gt;right = pop();
              temp-&gt;left = pop();
              push(temp);
              printf(&quot;%c&quot;,temp-&gt;entry);
          }
      }
  
      et = pop();
      return et;
  }
  
  int main(void){
      char postfix[] = &quot;ab+&quot;;
      TreeNode expTree = createExpTree(postfix);
      inOrder(expTree);
      preOrder(expTree);
      postOrder(expTree);
  }
      
  </code></pre>
  <p>I keep getting <em>Segmentation fault</em> , I cant figure out whats causing this error.
  How do I resolve this error ? please help.</p>
  <p><strong>EDIT:</strong></p>
  <p>I first changed the <em>newNode function</em> :</p>
  <pre><code>TreeNode * newNode(TreeEntry v){
      TreeNode * temp = (TreeNode *)malloc(sizeof(struct treenode));
      if(temp){
          temp-&gt;left = NULL;
          temp-&gt;right = NULL;
          temp-&gt;entry = v;
      }
      return temp;
  }
  </code></pre>
  <p>even after this change I was still getting Segmentation fault error.</p>
  <p>I managed to fix the error , apparently the error was in the <em>inorder</em> function in the TreeNode.h file</p>
  <p>I changed the inorder function to:</p>
  <pre><code>void Inorder(TreeNode *root, void(*Visit)(TreeEntry x))
  {
      if (root)
      {
          Inorder(root-&gt;left, Visit);
          Visit(root-&gt;entry);
          Inorder(root-&gt;right, Visit);
      }
  }
  </code></pre>
  <p>and changed the main.c file :</p>
  <pre><code>void printEntry(TreeEntry te){
      printf(&quot;%c&quot;,te);
  }
  
  
  int main(void){
      char postfix[] = &quot;ab+&quot;;
      TreeNode* expTree = createExpTree(postfix);
      Inorder(expTree,printEntry);
      
  }
  </code></pre>
  <p>Now I'm assuming the previous inorder function was unable to access the values of the pointer that was passed to it, and that was what caused the error,</p>
  <p>what is the reason for this ? I would be thankful if someone could give an explanation.</p>