1

In C, I find that the following produces different results although in my mind they should do the same thing:

Say I have the function int main (int argc, char* args[]) and I want to switch on the value of the second character of the first argument.

This works as expected:

args[1]++; 
switch (*args[1]) 
{
//...

But this doesn't (always falls back to default:)

switch (*(args[1]++))
{ 
//...

Could a 'C' master explain what's going on?

travisjayday
  • 784
  • 6
  • 16
  • 1
    Please provide a [MCVE]. What is `args[]`? What do you expect the result of `args[1]` to be? Why would you expect that `args[1]` and `*(args[1]++)` should have the same value? – ad absurdum Aug 26 '17 at 02:28
  • Ok thanks for the tip @David Bowling, look at my edit – travisjayday Aug 26 '17 at 02:30
  • 3
    Why do you think they should be the same? There's a dereference in the second case, while there's no dereference in the first case. There's also a post-increment that completes in the first case, and waits to complete in the second case. – Sergey Kalinichenko Aug 26 '17 at 02:32
  • type of `args[1]` is `char*`. In that case `switch(..){...}` thinks that expected result should not be obtained. – BLUEPIXY Aug 26 '17 at 02:32
  • Sorry for the confusion, I forgot to dereference in the first example but second example still doesn't work. To get the second character I access the `char*` add 1 to it and then dereference it. What's wrong with that? – travisjayday Aug 26 '17 at 02:33
  • 2
    `*(args[1]++)` --> `*++argv[1]` – BLUEPIXY Aug 26 '17 at 02:39
  • Thanks that works! But it still seems strange to me (by my own logic which is obviously flawed ;D) that even with parenthesis--meaning everything inside them should be executed first--the post incrementor didn't work as expected. – travisjayday Aug 26 '17 at 02:52
  • In this case parentheses are used to change the connection rather than the evaluation order. – BLUEPIXY Aug 26 '17 at 02:56
  • @travisjayday: Post increment returns the old value – AndyG Aug 26 '17 at 03:01
  • @BLUEPIXY "parentheses are used to change the connection rather than the evaluation order," could you elaborate on that? That's probably what I'm failing to understand. When I see parenthesis I immediately think: "yep, that must determine the order of evaluation." If you'd like to put that in an answer I'd be glad to accept it. – travisjayday Aug 26 '17 at 03:05
  • 1
    @travisjayday-- note that, given `int x = 1;` the expression `x++` evaluates to `1`, and then the value of `x` is incremented to `2` as a side-effect. – ad absurdum Aug 26 '17 at 03:14
  • Ah ok, that makes sense now. I misinterpreted the power of of '(' and ')' – travisjayday Aug 26 '17 at 03:16
  • E.g. `*argv[1]++` is possible as two possibilities like `(*argv[1])++` (Increment the value) and `*(argv[1]++)` (Increment the pointer) are considered. In the case without parentheses, it is evaluated like `*(argv[1]++)`. This means that the pointer is incremented after returning the current value. Since this is the default, parentheses are not necessary but may be added for clarification. Parentheses are required to evaluate like `(*argv[1])++`. In this way, parentheses are added to change the join of operators. It is not in order of evaluation. – BLUEPIXY Aug 26 '17 at 03:21
  • _In this case parentheses are used to change the connection rather than the evaluation order._ this explanation was not good. – BLUEPIXY Aug 26 '17 at 03:45
  • @travisjayday all you have here is a contrived example about misunderstanding what `a++` does when all you need is `++a` – Antti Haapala -- Слава Україні Aug 26 '17 at 04:42

0 Answers0