Ok. Let me start again. I am trying to write a program that would evaluate reverse Polish notation arguments to it. (i.e. ./program 2 4 +, which evaluates to 6).
int main(int argc, char *argv[])
{
char oper[MAXOP];
char *ptr;
unsigned char c;
double op2;
while (--argc > 0 && ++argv != NULL) {
ptr = oper;
if (isdigit(c = **argv))
{
while (isdigit(c = **argv++))
{
*ptr++ = c;
}
*ptr = '\0';
push(atof(oper));
}
else if (isalpha(c))
{
while (isalpha(c = **argv++))
{
*ptr++ = c;
}
...
...
}
}
The program should check the first character of each arguments if it is alpha or digit or an operator but it always turns out to be an alpha.
+++++++++ UPDATE +++++++++++++
Well, that’s odd. It is now working. Before the condition,
isdigit(c = **argv) wasn’t evaluating to true for numeric arguments (didn’t matter if the rest of the code was wrong). Now it does, but the condition isdigit(c = **argv++) is wrong.
It should be isdigit(c = *argv[0]++).
I had two files of the same name. They were in different directories but contain the same code. While working with this one, I must have been in the other directory and compiling the other file, and thus getting the same output despite edits to this one. My bad.
Have you tried using a debugger and figuring out what happens?
My first guess would be that
**argv++is wrong (are you increasing argv? *argv? **argv? Even if it’s not wrong, it’s just confusing code). Switch to:And go over cur_arg (using cur_arg++ to get to the next character, and not c=*cur_arg++ – again, confusing code).