1

I'm reading from a byte array as follows:

int* i = (int*)p;
id = *i;
i++;

correct me if I'm wrong, but ++ has precedence over *, so is possible to combine the *i and i++ in the same statement? (e.g. *i++)

(this is technically unsafe C#, not C++, p is a byte*)

toasteroven
  • 2,700
  • 3
  • 26
  • 35
  • 4
    Yes, but _please_ do not. Your code is perfectly easy to read, understand, modify and debug. *i++ is none of the above because in order to understand whether it means (*i)++ or *(i++), you need to correctly remember the precedence rules. – Eric Lippert Jul 07 '09 at 19:49
  • fair enough, and I would have to agree. just because it can be done, doesn't mean it should :) – toasteroven Jul 07 '09 at 19:52
  • It is idiomatic in C/C++ to write *i++, but it's definitely not so in C#. – Pavel Minaev Jul 07 '09 at 20:34

2 Answers2

2

I believe that

id = *i;
i++;

and

id = *i++;

are equivalent.

The ++ operator, when used as a suffix (e.g. i++), returns the value of the variable prior to the increment.


I'm somewhat confused by the reflector output for

unsafe class Test
{
    static public void Test1(int p, out int id)
    {
        int* i = (int*)(p);
        id = *i;
        i++;
    }

    static public void Test2(int p, out int id)
    {
        int* i = (int*)(p);
        id = *i++;
    }
}

which comes out as

public static unsafe void Test1(int p, out int id)
{
    int* i = (int*) p;
    id = i[0];
    i++;
}

and

public static unsafe void Test2(int p, out int id)
{
    int* i = (int*) p;
    i++;
    id = i[0];
}

which clearly are not equivalent.

Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
2

id = *i++

will do what you want.

++ modifies the pointer after the dereference.

EDIT: As Eric points out, per the spec, ++ does not happen after dereference. i++ increments i and return its initial value, so the spec defined behavior is the increment happens prior to dereference. The visible behavior of id = *i++ is the same whether you view the increment happening before or after dereference.

Michael
  • 54,279
  • 5
  • 125
  • 144
  • Actually, you have to be really careful when making that statement. It APPEARS that the pointer is modified after the dereference, but that is not actually true! Remember, "after" implies that there is a _sequence of events in time_. It is character building to work out what the exact sequence of events in time is for this case. When you do so, you'll see that in fact the variable modification happens BEFORE the dereference. – Eric Lippert Jul 07 '09 at 21:45
  • What is more: MSDN, in "Precedence and Order of Evaluation", explicitly states that operators with higher priority are evaluated before operators of lower priority. So, the evaluation order depends not on a whim of the compiler, the optimizer or an operator overload, it is strictly defined by the language. Increment first, then dereference, but using the old pointer value. It becomes a bit more complicated when you do something like this: a = *i++ + 2 * *i++. This can be seen as one-line spaghetti, but it's well defined :) – Rolf Dec 05 '14 at 08:18