Converting characters into integers

I need to convert a character to an integer, but it won't work.

My array pfExpression has both numbers and characters....as such, it is a character array (it is a postfix expression).

I want to convert the numeric values of it into integers, so I can perform mathematical operations on them.

This is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  for (int i = 0; i < pfLength; i++)
        {
                if(pfExpression[i] != '*' && pfExpression[i] != '/' && pfExpression[i] != '+' && pfExpression[i] != '-')
                {

                        int_pfExpression[j] = atoi(pfExpression[i]);
                        j++;
        }       } 


int atoi (char c)
{
        int i = c;
        i = i - 48;
        return (i);
}


I get the following error message which makes no sense to me:

stackDriver.cpp: In function âint evaluatePostFix(Stack, char*, int)â:
stackDriver.cpp:315:46: error: invalid conversion from âcharâ to âconst char*â [-fpermissive]
/usr/include/stdlib.h:148:12: error:   initializing argument 1 of âint atoi(const char*)â [-fpermissive
]
Do you have a forward declaration for atoi ?

It looks like the compiler is using the declaration of atoi from <cstdlib> which is:
int atoi (const char * str);
In main, I have:

int atoi(char);

Main calls the function that evaluates the postfix expression, which is the function I partially posted
I'd suggest changing the name of your function to something like a_to_i() so it doesn't conflict.
Ohhhhhhhh, I see.

I called the function before I defined it, like you said.

Thanks a lot! Awesome that you spotted that!
Last edited on
Topic archived. No new replies allowed.