-2

I ran into this in a library with the compiler:

register char *bufptr;
register int   neg = val < 0;
register long  uval = val;

*(bufptr = &tempc[BUFLEN - 1]) = 0;

do {*--bufptr = abs(uval % 10) + '0';}  while(uval /= 10);

What does "*--" actually do? I tried searching for that but it's not included in C references I could find and search engines do not like "*--" much.

Barleyman
  • 190
  • 11
  • 5
    It's two operators - think of `*--bufptr` as `*(--bufptr)`. – Paul R Dec 14 '16 at 13:43
  • 1
    Pointer dereference (*) and pre-decrement (--) – byxor Dec 14 '16 at 13:44
  • 3
    That's an example of "ugly" code. – haccks Dec 14 '16 at 13:44
  • @PaulR Maybe it's a daft question but why on earth is it "off topic"? – Barleyman Dec 15 '16 at 13:40
  • @Barleyman: the 5 close votes were probably for different reasons - off-topic ("resolved in a manner unlikely to help future readers") was the majority vote though, so that's the one that's shown above. I think that was probably my close reason too, since it was just a simple misunderstanding in the reading of two operators as a single operator, the resolution of which is unlikely to be of benefit to anyone else in the future. The fact that it's closed doesn't actually matter too much, in that the questioner got their answer - it just means that the bots will come along later and clean up. – Paul R Dec 15 '16 at 13:45
  • @PaulR From some googling it seems I'm hardly the only one confused by some of the possible combinations. In this particular case breaking those operations into *(bufptr--) makes the code much more readable. I'm a big believer into there's not being shortage of lines in source code so when I rewrote that routine I broke (*bufptr)++ and bufptr++ to separate items. With a 16MHz microcontroller simple utoa function needs some thought instead of tossing a modulo around as if the CPU had a DIV instruction. – Barleyman Dec 16 '16 at 15:46
  • @Barleyman: if you think that's confusing then take a look at the slide operator: `while (i --> 0) ...`. See [this question](http://stackoverflow.com/q/20754582/253056) for an explanation. ;-) – Paul R Dec 16 '16 at 16:12

2 Answers2

5

*--bufptr is equivalent of *(--bufptr), i.e. it decrements bufptr first and then gets the value of it.

If you look into the precedence table of C operators then you would see that increment and decrement operators have higher precedence that dereference operator.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • Ok, I see it. I wasn't actually aware of pre-decrement and why it exists. – Barleyman Dec 14 '16 at 13:56
  • 1
    @Barleyman if you didn't aware pre-decrement operator then I would suggest you to pick up a good C book. – taskinoor Dec 14 '16 at 13:58
  • It's fairly easy to find a reference that doesn't cover that. You don't exactly run into it much in source code either. Certainly not in the firmware I write for obvious reasons. – Barleyman Dec 14 '16 at 14:07
1

It's just a sequence of * and -- without a space; it's the same as *(--bufptr).

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38