title: Creating a recursive function that creates a expression tree from a prefix expression (in a char array) in C
body: <p>Each element (operands, operators, parenthese) in the prefix expression is seperated by a white space.
  These are some of the functions needed.</p>
  <pre><code>void createExpTree ( BTNode ** root , char * prefix );
  void printTree ( BTNode * node );
  void printTreePostfix ( BTNode * node );
  </code></pre>
  <p>and this is the structure of the BTnode</p>
  <pre><code>typedef struct _btnode{
  int item;
  struct _btnode *left;
  struct _btnode *right;
  } BTNode;
  </code></pre>
  <p>this is my code for the function createExpTree but I dont understand what is wrong</p>
  <pre><code>void createExpTree(BTNode** root,char* prefix)
  {
  int j, x;
  char c;
  char d[SIZE];
  
  static int count=0;
  
  c = prefix[count];
  if (count == strlen(prefix)) {
      return;
  }
  if (c == ' ')
      count++;
  c = prefix[count];
  if (c=='*' || c=='/' || c=='+' || c=='-') {
      // if prefix exp is OPT
      // create children
      BTNode* nn = (BTNode*)malloc(sizeof(BTNode));
      nn-&gt;left = (BTNode*)malloc(sizeof(BTNode));
      nn-&gt;right = (BTNode*)malloc(sizeof(BTNode));
      nn-&gt;item = (int)c;
      nn-&gt;left = NULL;
      nn-&gt;right = NULL;
      *root = nn;
      count++;
      createExpTree(&amp;((*root)-&gt;left), prefix);
      createExpTree(&amp;((*root)-&gt;right), prefix);
  }
  else { //if prefix exp is OPERAND
      j=0;
      while (c!=' ') {
          d[j]=c;
          j++; count++;
          c = prefix[count];
      }
      d[j]='\0';
      x = atoi(d);
      BTNode* nn = (BTNode*)malloc(sizeof(BTNode));
      nn-&gt;item = x;
      nn-&gt;left = NULL;
      nn-&gt;right = NULL;
      *root = nn;
      count++;
      return;
  }
  </code></pre>
  <p>}</p>
  <p>these are my codes for printTree and printTreePostfix</p>
  <pre><code>void printTree(BTNode *node){
  if (node == NULL) {
      return;
  }
  else {
      printTree(node-&gt;left);
      if(node-&gt;item &gt;= 0 || node-&gt;item &lt;= INT_MAX)
          printf(&quot;%d &quot;,node-&gt;item);
      else
          printf(&quot;%c &quot;,(char)(node-&gt;item));
      printTree(node-&gt;right);
  }
  }
  
  void printTreePostfix(BTNode *node){
  if (node == NULL) {
      return;
  }
  else {
      printTreePostfix(node-&gt;left);
      printTreePostfix(node-&gt;right);
      if(node-&gt;item &gt;= 0 || node-&gt;item &lt;= INT_MAX)
          printf(&quot;%d &quot;,node-&gt;item);
      else
          printf(&quot;%c &quot;,(char)(node-&gt;item));
  }
  }
  </code></pre>
  <p>The input for this question is something like &quot;+ 99 / + 88 77 - * 66 - 55 44 33&quot;, the integers are presumably 2-digit integers.</p>
  <p>Am I creating the expression tree correctly? I keep getting segmentation faults in codeblocks</p>
  <p>Would appreciate any help!! Thanks!!</p>
  <p>**Updated code: there still seems to be a segmentation fault after i fixed the prefix++ to prefix[count] with a static int count</p>