-3

Possible Duplicate:
Pointer Arithmetic: ++*ptr or *ptr++?

I do not understand what the difference is? Here is an example of a code that implements *ptr++

#include <stdio.h>
int my_array[] = {1,23,17,4,-5,100};
int *ptr;
int main(void)
{
int i;
ptr = &my_array[0]; /* point our pointer to the first
element of the array */
printf("\n\n");
for (i = 0; i < 6; i++)
{
printf("my_array[%d] = %d ",i,my_array[i]);
printf("ptr + %d = %d\n",i, *ptr++);

}
return 0;
}

The output is

my_array[0] = 1 ptr + 0 = 1
my_array[1] = 23 ptr + 1 = 23
my_array[2] = 17 ptr + 2 = 17
my_array[3] = 4 ptr + 3 = 4
my_array[4] = -5 ptr + 4 = -5
my_array[5] = 100 ptr + 5 = 100

When you change the second printf statement to printf("ptr + %d = %d\n",i, *(++ptr)); this becomes the ouput:

my_array[0] = 1 ptr + 0 = 23
my_array[1] = 23 ptr + 1 = 17
my_array[2] = 17 ptr + 2 = 4
my_array[3] = 4 ptr + 3 = -5
my_array[4] = -5 ptr + 4 = 100
my_array[5] = 100 ptr + 5 = -1881141248

Someone please explain the difference in detail so I can understand.

Community
  • 1
  • 1
Mike Smith
  • 67
  • 3
  • 9
  • 1
    `*++ptr` increments, then returns the *current* eval, `*ptr++` increments, then returns the *prior* eval. This is covered on a number of [questions](http://stackoverflow.com/questions/5209602/pointer-arithmetic-ptr-or-ptr) and [answers](http://stackoverflow.com/questions/13338730/vs-precedence-in-c/13338801#13338801), to name just a few. – WhozCraig Dec 22 '12 at 01:03

3 Answers3

3

One increments the pointer BEFORE fetching what it points to, the other increments AFTER fetching from the pointer.

In the second example, you have stepped past the end of your array the last iteration, and you are printing (probably) the pointer that is in the memory location immediately after your array (or some random garbage)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
3

This stands no chance of an upvote because this question is about to be closed anyway, but I am compelled to put it here anyway.

When using *ptr++ the following happens:

  1. Make a copy of the existing value of ptr
  2. Increment ptr by type-width bytes (in this case one, since char is one byte)
  3. Derference the prior value from the copy made in (1)

When using *++ptr the following happens:

  1. Increment ptr by type-width bytes (in this case one, since char is one byte)
  2. Dereference ptr `s new value.

The fundamental copy-then-increment requirement of post-increment is vital for performance when overriding said-operators on C++ objects where temp-copying can be extremely expensive.


Post-Increment

The following demonstrates the post-increment behavior for anyone that doubts this:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

static char test[] = "AB";
static const char *ptr = test;

void myprintf(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    printf("myprintf: %c\n", *ptr);
    vprintf(fmt, args);
}

int main(int argc, char *argv[])
{
    myprintf("main: %c\n", *ptr++);
    return EXIT_SUCCESS;
}

Output

myprintf: B
main: A

Note the value of ptr has already been incremented in main() prior to the invoke of myprintf(), contrary to what most people think, and likewise contrary to what most C/C++ instructors and books apparently teach. A disassembly of this proves this to be the case:

movq    _ptr(%rip), %rsi     ; ptr value moved into rsi
movq    %rsi, %rcx           ; ptr value moved into rcx
addq    $1, %rcx             ; increment value by one
movq    %rcx, _ptr(%rip)     ; ** store incremented address back into ptr **
movsbl  (%rsi), %esi         ; old pointer value still in rsi dereferenced here.
movq    %rax, %rdi           
movb    $0, %al              
callq   _myprintf
movl    $0, %eax

Pre-Increment

The same code above, but using pre-increment, meaning change the single call in main() to this:

myprintf("main: %c\n", *++ptr);

Output

myprintf: B
main: B
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
1

One is the pre-increment operator and the other is the post-increment operator.

printf("%d", i++); is the same as:

printf("%d", i);
i += 1;

While printf("%d", ++i); is the same as:

i += 1;
printf("%d", i);
nybbler
  • 4,793
  • 28
  • 23