views:

82

answers:

3

Somebody write this function

void strToUpper(char *p) {
  while (*p) {
    *p = TOUPPER(*p);
    *p++;                //<-- Line to pay attention
  }
}

I asked, why do you put the * before p++?

answer: because "It's the same", I corrected the code and then stay angry for a while, because both really work the same...

void strToUpper(char *p) {
  while (*p) {
    *p = TOUPPER(*p);
    p++;                //<-- Line to pay attention
  }
}

So I would like to share this with stackoverflow, example:

char s[6]="hello";

What would it do?

*++ptr;

This will evaluate the ++ pre-increment(over the pointer) and then the dereference operator *, so it will let a char value 'e' (second letter of "hello") (that is not used in this case and could generate a warning on compilation) and the pointer will be pointing from the 'e' (position 1)

What would it do?

*ptr++;

This is a little weird because it will evaluate the dereference operator * first, so it will let a char value 'h' (that is neither used in this case), and then the ++ post-increment(to the pointer), so (again) the pointer will be pointing from the 'e' (position 1)

What would it do?

ptr++;

Finally it won't have a Lvalue of char, but it won't generate any warning if isn't used, and the pointer will be pointing from the 'e' (position 1) too.

These three forms does the same from the pointer address point of view..

IMHO That's the worst thing of some computer languages (almost everyone)..

"There is a poor Hamming distance between any code and any bug"

We have no redundancy when programming, if you take a Law book, and write random chars within, It will be readable, but if you type random when programming, you get a bug, 100% accuracy

+1  A: 

++ has precedence over *. But I agree, it's unclear when you mix both in the same statement.

Alexandre C.
For the sake of our health, we (programmers) would need some more human like, fail tolerant language... I think future will bring it
Hernán Eche
+2  A: 

According to the operator precedence in C, ++ will evaluate first, and only then *. So in *p++, ptr will get its value inceremented, and then the statement will get the value of the opbject to which ptr is now pointing to. Of course, this isn't different from ptr++, since you aren't using the value of the statement.

Tomer Vromen
In the case of *p++, the ++ is a post-increment, so it will use the value of p, dereference it, THEN increment the pointer, p. In other words, *p++ returns 'h', then moves p to point to the 'e'.
Scott Thomson
@Scott: That's a common misconceptin. No expression can yield a result first and then somehow have a side effect later (I'm ignoring sequence points here). If you overload operator++ for your own types, you will understand that the side effect happens first, and then a result is returned, it's just that the result is the old pointer.
FredOverflow
+1  A: 

Anyone who mixes ++, --, += etc with other operators has already failed writing readable code, the fact that they'll bug it out is nothing more than their due.

DeadMG
Agree!, parenthesis like condoms, it's safer to use them " () "
Hernán Eche
they may also trigger undefined behaviour, like a = a++ (a lot of C n00bs do write this at some point)
Alexandre C.