can anyone find why is this program to solve postfix expression giving runtime error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    can anyone find why is this program to solve postfix expression giving runtime error

    i am trying to write a program to solve postfix expression but it is giving runtime error.i tried to debug but i am not getting why is it happening can anyone find out.

    #include< stdio.h>

    #include< string.h>

    #include< stdlib.h>

    struct stack{

    int top;

    int n[100];

    }s;

    int push(char a[]) {

    int i,j;

    int p=atoi(a);

    s.n[s.top+1]=p;

    s.top++;

    for(i=0;i<100;i ++)

    a[i]=0;

    j=0;

    return(j);

    }

    void pop(char a[])

    {

    int c,b;

    b=s.n[s.top];

    c=s.n[s.top-1];

    s.top--;

    switch(a[0])

    {

    case '+':

    s.n[s.top]=b+c;

    break;

    case '-':

    s.n[s.top]=b-c;

    break;

    case '*':

    s.n[s.top]=b*c;

    break;

    case '/':

    s.n[s.top]=b/c;

    break;

    }
    a[0]=0;
    }

    int main()

    {

    s.top=-1;

    int m,i,k;

    char a[100],c[100];

    scanf("%d",&m);

    for(i=0;i<m;i++ )
    {
    int j=0;
    while(1)
    {
    scanf("%c",&a[j]);
    if(a[j]=='?')
    break;
    else if(a[j]==' ')
    j=push(a);
    else if(a[j]=='+'||'-'||'*'||'/')
    pop(a[j]);
    else
    j++;
    }
    printf("%d",s.n[s.top]);
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There is this code:

    Code:
    int push(char a[]) {
    
     int i,j;
    
     [B]int p=atoi(a);   <----
    [/B]
    Here a is an array. The name of an array is the address of the array. So this code converts the address of the array to an int. Is that what you wanted? It looks suspicious.

    You have simply got to step through this code using your debugger. You can verify the values of your variables every step of the way.

    Comment

    • jinnejeevansai
      New Member
      • Nov 2014
      • 48

      #3
      Then how to convert an array of characters to integer.I have read that it was correct syntax.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Here is where you need to be accurate in your thinking. An argument of char a[] means a char array. Unfortunately, the size of the array is unknown. So char a[] is maybe an array of 1 char or maybe larger.

        When this is passed to atoi, any leading whitespace characters are skipped. Then a + or - is processed for the sign of the int.

        The next characters must be digits 0-9 and and contiguous. Conversion stops after the last decimal digit.

        So, if atoi can't convert it returns 0. Now you can't tell if the array had a zero or that atoi failed. Not good. Further, atoi may run off the end of your array.

        atoi does not change the array. So if you had 5-3, you end up with two ints 5 and -3, which then ADD to get 2. You can't parse the array, find the - sign and do 5 - (-3) or you will get 8.

        As long as you know what's in the array atoi will work.

        All of these issues can be resolved by using your debugger.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Some compilers can be set for different levels of pedantry and warning-verbosity. Set your compiler for maximum verbosity.
          Do you have any compiler warnings?
          What precisely is the nature of the run-time error?
          These are clues that may lead to the source of the problem.

          Comment

          Working...