title: Evaluate a postfix expression using a stack and array in C
body: <p>I am still new and not too quick on picking up coding with C. For an assignment I have to evaluate a Postfix expression from an array using a stack. While I am sure I have several problems with my code I feel the basic structure is good. It compiles without errors, but does have some warnings. It will run and printout the printf statements in main but will not do anything in the evaluation as far as I can tell.
  I am not looking for a cheat or a complete fix but some guidance would be appreciated
  Please respond assuming I know very little.</p>
  
  <p>Cheers</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct stackNode {
      int data;
      struct stackNode *nextPtr;
  };
  typedef struct stackNode StackNode;
  typedef StackNode *StackNodePtr;
  
  int evaluatePostfixExpression( char *expr );
  int calculate( int op1, int op2, char operator );
  void push( StackNodePtr *topPtr, int value );
  int pop( StackNodePtr *topPtr );
  int isEmpty( StackNodePtr topPtr );
  void printStack( StackNodePtr topPtr );
  
  char postfix[50];
  int answer;
  
  
  
  void main()
  {
      printf("Print an postfix expression\n");
      scanf("%s", postfix);
      evaluatePostfixExpression(postfix);
      printf("The value of the expression is: %i",answer);
  }
  
  int evaluatePostfixExpression( char *expr )
      //Evaluate the postfix expression.
  {
  
      StackNode node;
      StackNodePtr ptrnode;
      ptrnode = &amp;node;
  
      int x;
      int y;
      int z;
  
      strcat(ptrnode,'\0');/*Append the null character ('\0') to the end of the postfix expression.
      When the null character is encountered, no further processing is necessary.*/
  
      int i=0;
      for(i; postfix[i]!='\0'; i++){      //While '\0' has not been encountered, read the expression from left to right.
          if(isdigit(postfix[i])){           //If the current character is a digit,Push its integer value onto the stack
              push(&amp;ptrnode, postfix[i]); //(the integer value of a digit character is its value in the computer’s character
              printStack(ptrnode);                           //set minus the value of '0' in the computer’s character set).
          }
          else if(postfix[i]=='+'||postfix[i]=='-'||postfix[i]=='*'||postfix[i]=='/'||postfix[i]=='^'){                   //Otherwise, if the current character is an operator, Pop the two top elements of the stack into
              x=pop(&amp;ptrnode);    //variables x and y. Calculate y operator x.
              printStack(ptrnode);
              y=pop(&amp;ptrnode);
              printStack(ptrnode);
              z=calculate(x,y,postfix[i]);
              push(&amp;ptrnode, z);          /*Push the result of the calculation onto the stack.*/
              printStack(ptrnode);
          }
          if (postfix[i]=='\0'){  //When the null character is encountered in the expression, pop the top value of the
              answer = pop(&amp;ptrnode);//stack. This is the result of the postfix expression.
              printStack(ptrnode);
          }
      }
  }
  
  int calculate( int op1, int op2, char operator )
      //Evaluate the expression op1 operator op2.
  {
      if (operator=='+')
          return op1+op2;
  
      else if (operator=='-')
          return op1-op2;
  
      else if (operator=='*')
          return op1*op2;
  
      else if (operator=='/')
          return op1/op2;
  
      else if (operator=='^')
          return op1^op2;
  
  
  
      else{
      return printf("calculation error");
      }
  }
  
  void push( StackNodePtr *topPtr, int value )
      //Push a value on the stack.
  {
      StackNodePtr temp; /* to create a new node */
      temp =  malloc(sizeof(value));//need Malloc because it will not remember it
      temp-&gt;data = value;
      temp-&gt;nextPtr = NULL; /* the new node points to NULL */
  
  
      if(isEmpty(*topPtr)==0) {
      temp-&gt;nextPtr = *topPtr;
      }
  
  }
  
  int pop( StackNodePtr *topPtr )
      //Pop a value off the stack.
  {
      char Data ; /* to be used to store the data */
      StackNodePtr tmp; /* to be used for handling the node*/
                          /* that is going to be deleted */
      tmp = *topPtr; /* tmp has the address of the node */
                          /* that is going to be deleted */
      Data = tmp-&gt;data;
      *topPtr = tmp-&gt;nextPtr;
  
      free(tmp);
      return Data;
  }
  
  int isEmpty( StackNodePtr topPtr )
      //Determine if the stack is empty.
  {
      if(topPtr-&gt;nextPtr==NULL)
          return 1;
      else
          return 0;
  }
  
  void printStack( StackNodePtr topPtr )
      //Print the stack.
  {
      while ((topPtr-&gt;nextPtr)!=NULL){
          printf("%C",topPtr-&gt;data);
      }
      if ((topPtr-&gt;nextPtr)==NULL)
          printf("NULL");
  
      printStack(topPtr-&gt;nextPtr);
  }
  </code></pre>