Hi folks i tried to program a simple switch statement and stuck uppon this problem. I cant figure out why this code works correctly, i guess it is because the precedence of the operators ** and ++. If this is the case ill be happy if someone can write an example how should i use value-at * and inc/dec operator in a statement like below.
Thanks in advance,
while (--argc > 0)
argv++;
switch (**argv)
and this code dont
while (--argc > 0)
switch (**(argv++))
Full code:
while (--argc > 0) {
switch (**(argv++)) {
case '\0':
panic("empty command line argument");
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
push (atof(*argv));
break;
case '+':
push(pop() + pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '*':
push(pop() * pop());
break;
case '/':
op2 = pop();
push(pop() / op2);
break;
default:
panic("unknown command");
break;
}
}
Using the postincrement on a given variable in any expression causes the variable to increment after the expression has been evaluated.
As stated by your first, working code, where you increment argv
value before getting it's referenced value in the switch, you want the variable to be incremented before your expression is evaluated, so you need to use the preincrement:
while (--argc > 0)
switch (**(++argv))