Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between *ptr += 1 and *ptr++ in C

People also ask

What is the difference between * ptr and ptr?

ptr is the pointer itself. *ptr is the value it points to. &ptr is the address of the pointer.

What is the difference between * ptr ++ and ++* ptr?

So *ptr++ gets evaluated as *(ptr++) . ptr++ evaluates to the current value of ptr ; it increments ptr only as a side effect. The value of the expression is the same as the current value of ptr .

What is the difference between int * ptr and int * ptr?

there is no difference between int* ptr and int *ptr.

Are the expressions * ptr ++ and * ptr same?

Both operators have the same precedence, and right-left associativity. This means the expression will be grouped ++(*ptr) . The ++ part will be applied to the value of the *ptr part.


The difference is due to operator precedence.

The post-increment operator ++ has higher precedence than the dereference operator *. So *ptr++ is equivalent to *(ptr++). In other words, the post increment modifies the pointer, not what it points to.

The assignment operator += has lower precedence than the dereference operator *, so *ptr+=1 is equivalent to (*ptr)+=1. In other words, the assignment operator modifies the value that the pointer points to, and does not change the pointer itself.


The order of precedence for the 3 operators involved in your question is the following :

post-increment ++ > dereference * > assignment +=

You can check this page for further details on the subject.

When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expression *p++ is parsed as *(p++), and not as (*p)++.

Long story short, in order to express this assignment *ptr+=1 using the post-increment operator you need to add parentheses to the dereference operator to give that operation precedence over ++ as in this (*ptr)++


Let's apply parentheses to show the order of operations

a + b / c
a + (b/c)

Let's do it again with

*ptr   += 1
(*ptr) += 1

And again with

*ptr++
*(ptr++)
  • In *ptr += 1, we increment the value of the variable our pointer points to.
  • In *ptr++, we increment the pointer after our entire statement (line of code) is done, and return a reference to the variable our pointer points to.

The latter allows you to do things like:

for(int i = 0; i < length; i++)
{
    // Copy value from *src and store it in *dest
    *dest++ = *src++;

    // Keep in mind that the above is equivalent to
    *(dest++) = *(src++);
}

This is a common method used to copy a src array into another dest array.


Very good question.

In K&R "C programming language" "5.1 Pointers and Addresses", we can get an answer for this.

"The unary operators * and & bind more tightly than arithmetic operators"

*ptr += 1      //Increment what ptr points to.

"Unary operators like * and ++ associate right to left."

*ptr++        //Increment prt instead of what ptr point to.

//It works like *(ptr++).

The correct way is:

(*ptr)++      //This will work.