Written in C and C-like languages (like C++ and Perl) as a suffixed "++": x++. An expression which increments x as a side effect, but has its original value.

This is actually a very useful gadget. One idiom is where tbl is an array, being filled in. Currently, it has n_tbl of its values in use. To add some value val, use something like:

  if (n_tbl == tbl_sz) {
    /* Grow tbl to have tbl_sz > n_tbl+1 entries */
  }
  tbl[n_tbl++] = val;
(Users of Perl will smugly observe they have push for this purpose)

Here's strcpy, which copies one null terminated string to another (sufficient space must be allocated):

  char *strcpy(char *dest, char *src)
  {
    char *d = dest;         /* Must return dest */
    while (*dest++ = *src++)
      ;
    return d;
  }

In C++, postincrementing an object is often frowned upon, as it will cause a copy constructor call (to save the original value of x). When the copy constructor has some side effect, or it is not available to the compiler, or the compiler isn't extremely smart, it cannot be optimised out (even if "x++;" is being used purely for side effect!), yielding to inefficiency.

Log in or register to write something here or to contact authors.