-5

I'm rather new to C and sometimes I come across strange notations, especially in relation with pointers.

A very short example:

....
real *ptr;
real delta_force;
for(i<particles;...)
{
  ...some calculations
  ptr=&FORCE(i,...);  //FORCE is a macro returning the current force on a particle
  *(++ptr) += delta_force;
  ...
 }
...

How can I interpret *(++ptr)?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
OD IUM
  • 1,555
  • 2
  • 16
  • 26
  • 2
    It's the same as `++ptr; *ptr;`. Increment then dereference. – Cornstalks Jul 28 '16 at 13:30
  • 2
    Do you know what `++ptr` is ? And what the unary `*` is? Now combine them. – Eugene Sh. Jul 28 '16 at 13:30
  • Have you ever heard something about [pointer dereference](http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean). – LPs Jul 28 '16 at 13:30
  • the ++ is performed first, incrementing the address held in ptr. Then, the value of the variable at the address held in ptr is incremented by delta_force. – clarasoft-it Jul 28 '16 at 13:31

4 Answers4

8

First increment pointer and then add delta_force to the value pointed to by the pointer.

*(++ptr) += delta_force;

means same as

ptr = ptr + 1;
*ptr = *ptr + delta_force;
Markus Laire
  • 2,837
  • 2
  • 17
  • 23
  • got it, thanks a lot – OD IUM Jul 28 '16 at 14:03
  • 1
    Note: `ptr` is evaluated once with `*(++ptr)` rather additional times with other "equivalent" code. May make a difference should `ptr` be something more complex that a simple object/pointer, unlike OP's `real *ptr;`. – chux - Reinstate Monica Jul 28 '16 at 14:43
  • thanks again. actually ptr is pointing to a Force-Vector where the components are accessed via incrementing so that *(ptr) corresponds to the x-component, while *(++ptr) is the y-component and so forth... – OD IUM Jul 28 '16 at 15:15
2

Read it from inside out. *(++ptr) += somevalue Is equal to the following Code

++ptr; //increases the Pointer by the sizeof(real)
real v = *ptr; // dereferences the Pointer and assigns the value it is pointing to to v
v = v + somevalue; // increases v by somevalue
*ptr = v; // assigns the new value of v to the target of ptr
derpirscher
  • 14,418
  • 3
  • 18
  • 35
1

This is a combination of the increment operator ++ and the pointer dereference notation *

So first you are increasing by one the value of the address and then you are dereferencing your pointer to get its value.

In summary: you are going to the next pointer

renno
  • 2,659
  • 2
  • 27
  • 58
  • Well, `*(ptr++) += delta_force` also goes to the next pointer, but doesn't do the same thing than `*(++ptr) += delta_force`. In the cas of this question, you go to the next pointer, before doing the other operations present on the line. In the one I gave at the beginning of this comment, you go to the next pointer after doing the other operations on the line. – HolyDanna Jul 28 '16 at 13:36
  • You are right. I edited so it would be more clear what I said. – renno Jul 28 '16 at 13:39
  • got it, thanks a lot – OD IUM Jul 28 '16 at 14:03
1

in pointers in c programming language....(*) means 'value at adress of'

and here the pointer ptr contain the adress of FORCE macro so first the adress will be incremented then the value at adress of ptr will be updated to new value during each iterations of loop...

Amrit
  • 2,115
  • 1
  • 21
  • 41