0
votes

here is a simple program for postfix calculator using stack, but the atoi() causes it to crash. Why is it happening? I have tried converting the char to string using ch-'0' and it works but the atoi() function for char to int conversion does not seem to work in this case.

Is it because ch is a char nor string eg. char ch; and not char ch[20];

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int num[MAX],tos=-1;

push(int x)
{
    if(tos==MAX)
    {
        printf("the stack is full");
    }
    else
    {
        printf("  l");
        tos++;
        num[tos]=x;
    }
}
int pop()
{
    if(tos<0)
    {
        printf("stack underflow");
    }
    else
    return num[tos--];
}
int main()
{
    char postfix[MAX],exp[MAX],ch,val;
    int a,b;
    printf("enter the postfix expression");
    fgets(postfix,MAX,stdin);
    strcpy(exp,postfix);
    for(int i=0;i<strlen(postfix);i++)
    {
         printf(" xox ");
        ch=postfix[i];
        if(isdigit(ch))
        {
            push(ch - '0');
            printf(" %d ",atoi(ch));
        }
      else
      {
          printf("%d",tos);
          a=pop();
          b=pop();
          switch(ch)
          {
          case '+':
            val=a+b;
            break;
          case '-':
            val=a-b;
            break;
          case '*':
            val=a*b;
            break;
          case '/':
            val=a/b;
            break;
        }
        printf("%d",val);
        push(val);
      }
    }
    printf("the result of the expression %s = %d",exp,num[0]);
    return 0;
}
1
"Is it because ch is a char nor string eg. char ch; and not char ch[20];" That's right. It works on strings and there's no overload for just a single char. - Blaze
If you want to transform a char that represents a number from 0 to 9 to the respective integer, simply subtract '0' like you're already doing in your code. If you want it to keep the same value (so for instance '0' is 48 in ASCII), just assign the char to the int variable. - Blaze
atoi(ch) shouldn't even compile. Your compiler is either terribly bad or it is not configured correctly. - Lundin
Add default: printf("<%d>\n", ch); to see a problem. - chux - Reinstate Monica
Compile with all warnings enabled and treat them as errors, especially the warning displayed at the line containing atoi. Hint: atoi expects a pointer to a string, but you're passing a char. You're mixung up strings and chars, read the chapter dealing with strings in your C text book. - Jabberwocky

1 Answers

2
votes

Is it because ch is a char nor string eg. char ch; and not char ch[20];

Yes. atoi(ch) is not even valid C and is not allowed to compile cleanly.

In this case you can create a temporary string based on ch and a null terminator. For example through a compound literal: (char[2]){ch, '\0'}.

And you should never use atoi for any purpose, since it has poor error handling and is a completely superfluous function. Use the strtol family of functions instead.

You can call strtol like this:

strtol( (char[2]){ch, '\0'}, // string to convert from
        NULL,                // end pointer, not used, set to NULL
        10 );                // base 10 = decimal

Example:

printf(" %d ", (int)strtol( (char[2]){ch, '\0'}, NULL, 10) );

Which is completely equivalent to the more readable:

char tmp[2] = { ch, '\0' };
int result = (int) strtol(tmp, NULL, 10);
printf(" %d ", result);