Thread: code won't compile

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    code won't compile

    can anyone tell me where the errors are to get this thing to compile?

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    #define MAXSIZE 50                                        
    
    typedef struct {
       char elements[MAXSIZE];
       int count;
    } Stack;
    
    void initialize(Stack *stackPtr); 
    int isEmpty(Stack *stackPtr); 
    void push(Stack *stackPtr, int x); 
    void pop(Stack *stackPtr, int * x);  
    
    int main()
    {
       char PostfixExpression[MAXSIZE + 1];
       int LeftOperand, RightOperand;
       int Result;
       int i;
       char c;
       char s[MAXSIZE];
       int howlong;
       Stack EvaluateStack;
    
       initialize(&EvaluateStack);
    
       printf("Enter the postfix expression: ");
       fgets(PostfixExpression, sizeof(PostfixExpression), stdin);
    
       howlong = strlen(PostfixExpression);
    
       for(i = 0; i < howlong; i++)
       {
            c = PostfixExpression[i];
            s[0] = c;
            s[1] = '\0';
    
            PostfixExpression[i] = c;
    
            if(isdigit(c))
            {
               push(&EvaluateStack, (int)atof(s));
            }
            else if(c == '+' || c == '-' || c == '*' || c == '/') 
            {
              pop(&EvaluateStack, &RightOperand);
              pop(&EvaluateStack, &LeftOperand);
            }
            if(c == '+')
             { Result = (LeftOperand + RightOperand);
               push(&EvaluateStack, &Result);
             }
            else if(c == '-')
             {
               Result = (LeftOperand - RightOperand);
               push(&EvaluateStack, &Result);
             }
            else if(c == '*')
             {  
               Result = (LeftOperand * RightOperand);
               push(&EvaluateStack, &Result);
             }
            else
             {
               Result = (LeftOperand / RightOperand);
               push(&EvaluateStack, &Result);
             }
                
               
            }
       }
          
       pop(&EvaluateStack, &Result);
       printf("The value of the expression is: %d.\n", Result);
    
       return 0;
    }                              
    
    void initialize(Stack *stackPtr)
    {
         stackPtr->count = 0;
    } 
    
    int isEmpty(Stack *stackPtr)
    {
         if(stackPtr->count == 0)
    	    return 1;
    	 else
            return 0;
    }
    
    int isFull(Stack *stackPtr)
    {
         if(stackPtr->count == 50)
           return 1;
         else
           return 0;
    } 
    
    void push(Stack *stackPtr, int x)
    {
         if(stackPtr->count == MAXSIZE)
    	 {
           printf("The stack is full, aborting...\n");
           return;
           }
           else
           {
              stackPtr->elements[stackPtr->count] = x;
              stackPtr->count++;
           }
    }
    
    void pop(Stack *stackPtr, int *x)
    {
          if(stackPtr->count == 0)
          {
             printf("The stack is empty, cannot delete.\n");
             exit(0);
          }
    	else
    	{
    	   stackPtr->count--;
    	   *x = stackPtr->elements[stackPtr->count];
    	}
    	
            return;
    }
    Here are the compile errors I get but I can't figure out what is wrong on these lines:

    Code:
    In function `main':
    61: warning: passing arg 2 of `push' makes integer from pointer without a cast
    66: warning: passing arg 2 of `push' makes integer from pointer without a cast
    71: warning: passing arg 2 of `push' makes integer from pointer without a cast
    76: warning: passing arg 2 of `push' makes integer from pointer without a cast
    At top level:
    83: error: parse error before '&' token
    83: error: conflicting types for 'pop'
    22: error: previous declaration of 'pop' was here
    83: error: conflicting types for 'pop'
    22: error: previous declaration of 'pop' was here
    83: warning: data definition has no type or storage class
    84: error: parse error before string constant
    84: error: conflicting types for 'printf'
    84: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    84: error: conflicting types for 'printf'
    84: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    84: warning: data definition has no type or storage class
    125: error: conflicting types for 'pop'
    83: error: previous declaration of 'pop' was here
    125: error: conflicting types for 'pop'
    83: error: previous declaration of 'pop' was here

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    no idea what compielr are you using?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Location
    Stockholm, Sweden
    Posts
    64
    On line 15 you define push as taking a pointer to a stack and an integer value. On lines 55,60,65 and 70 you pass two pointers to the function.

    This is the cause of the warning: passing arg 2 of `push' makes integer from pointer without a cast message.

    On line 73 you have a closing brace which doesn't match up. It closes the main function, which means the lines 74 - 79 become syntax errors.

    Remove that brace and the code compiles (it does on MSVS 2003 anyway), but it craps out if you try to enter any expresson including an operator. I.E "12345" works fine (it just returns 0 as the result) but entering 12+ crashes the program.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    well, thanks for helping me getting it to compile correctly. do you see where the fault is in my code? I don't see where it is doing anything wrong.

    I created a program with a switch statement, it compiles correctly and does most operations correctly. the only problem with it, is for some reason when I enter something like "4 5 6 + *" as the postfix expression, it returns the value 44. I'm not sure on this but I think it would be returning 54. it should be evaluation (4+5) and then multiplying the result by 6 if I have the definition of a postfix expression correct. instead, the program is doing (4+6)*4. please tell me if I am wrong. I will post the code I used to get this result. I figured there was something wrong with my switch statement is the reason I modified it to the code above. Do you know why it isn't evaluating the expressions correctly (it actually could be correct because I am not totally sure about postifx expressions)? It seems that it should be just some simple mistake which I am overlooking. Here is the previous code:

    Update: Sorry. I think this code may be correct. I did some research online and found other postfix calculators and they get the same results as my program does. Thanks for the help on compiling the first program though.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSIZE 50
                        
                        
    typedef struct {
       char elements[MAXSIZE];
       int count;
    } Stack;
    
    void initialize(Stack *stackPtr); 
    int isEmpty(Stack *stackPtr); 
    void push(Stack *stackPtr, int x); 
    void pop(Stack *stackPtr, int * x);  
    
    
    int main()
    {
    
       char PostfixExpression[MAXSIZE + 1];
       int LeftOperand, RightOperand, Result;
       int i;
       char c;
       char s[MAXSIZE];  
       int howlong;
       Stack EvaluateStack;
    
    
       initialize(&EvaluateStack);
    
       printf("Enter the postfix expression: ");
       fgets(PostfixExpression, sizeof(PostfixExpression), stdin); 
       
       howlong = strlen(PostfixExpression);
    
       for(i = 0; i < howlong; i++)
       {  
          c = PostfixExpression[i];
    
          s[0] = c;
          s[1] = '\0';
          
          PostfixExpression[i] = c;
          
          if(isdigit(c))
          {
             push(&EvaluateStack, (int)atof(s));
          }
          else if(c == '+' || c == '-' || c == '*' || c == '/')
          {
             pop(&EvaluateStack, &RightOperand); 
             pop(&EvaluateStack, &LeftOperand);
             
             switch(c) {
                case '+' : push(&EvaluateStack, (LeftOperand + RightOperand));
                           break;
                case '-' : push(&EvaluateStack, (LeftOperand - RightOperand));
                           break; 
                case '*' : push(&EvaluateStack, (LeftOperand * RightOperand));
                           break;                     
                case '/' : push(&EvaluateStack, (LeftOperand / RightOperand));
                           break; 
                default: break;
             }
          }
       }
       
       pop(&EvaluateStack, &Result);
       printf("The value of the expression is: %d.\n", Result);
       
       
       return 0;
    }
                                  
    
    
    void initialize(Stack *stackPtr)
    {
         stackPtr->count = 0;
    }
    
    
     
    int isEmpty(Stack *stackPtr)
    {
         if(stackPtr->count == 0)
    	    return 1;
    	 else
            return 0;
    }   
    
    
    int isFull(Stack *stackPtr)
    {
         if(stackPtr->count == 50)
           return 1;
         else
           return 0;
    }
    
     
    void push(Stack *stackPtr, int x)
    
    {
         if(stackPtr->count == MAXSIZE)
    	 {
           printf("The stack is full, aborting...\n");	
           return;
         }
         else
         {
           stackPtr->elements[stackPtr->count] = x;
           stackPtr->count++;
         }
    }
    
     
    void pop(Stack *stackPtr, int *x) 
    {
      
        if(stackPtr->count == 0)
        {
           printf("The stack is empty, cannot delete.\n");
           exit(0);
        }
    	else
    	{
    	   stackPtr->count--;
    	   *x = stackPtr->elements[stackPtr->count];
    	}
    	
    	return;
    }
    Last edited by the_winky_files; 11-14-2005 at 11:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code won't compile
    By monkles in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 01:45 PM
  2. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 09:36 AM
  3. This code won't compile in Red Hat Linux 9
    By array in forum Linux Programming
    Replies: 7
    Last Post: 04-27-2004, 06:30 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  5. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM