2 <clause number="25.5.6" title="Pointer arithmetic">
3 <paragraph>In an unsafe context, the + operator (<hyperlink>14.7.4</hyperlink>) and -operator (<hyperlink>14.7.5</hyperlink>) can be applied to values of all pointer types except void*. Thus, for every pointer type T*, the following operators are implicitly defined: <code_example><![CDATA[
4 T* operator +(T* x, int y);
5 T* operator +(T* x, uint y);
6 T* operator +(T* x, long y);
7 T* operator +(T* x, ulong y);
8 T* operator +(int x, T* y);
9 T* operator +(uint x, T* y);
10 T* operator +(long x, T* y);
11 T* operator +(ulong x, T* y);
12 T* operator -(T* x, int y);
13 T* operator -(T* x, uint y);
14 T* operator -(T* x, long y);
15 T* operator -(T* x, ulong y);
16 long operator -(T* x, T* y);
17 ]]></code_example></paragraph>
18 <paragraph>Given an expression P of a pointer type T* and an expression N of type <keyword>int</keyword>, <keyword>uint</keyword>, <keyword>long</keyword>, or <keyword>ulong</keyword>, the expressions P + N and N + P compute the pointer value of type T* that results from adding N * sizeof(T) to the address given by P. Likewise, the expression P -N computes the pointer value of type T* that results from subtracting N * sizeof(T) from the address given by P. </paragraph>
19 <paragraph>Given two expressions, P and Q, of a pointer type T*, the expression P -Q computes the difference between the addresses given by P and Q and then divides that difference by sizeof(T). The type of the result is always <keyword>long</keyword>. In effect, P -Q is computed as ((<keyword>long</keyword>)(P) -(<keyword>long</keyword>)(Q)) / sizeof(T). </paragraph>
21 <example>[Example: For example: <code_example><![CDATA[
27 int* values = stackalloc int[20];
32 Console.WriteLine("p - q = {0}", p - q);
33 Console.WriteLine("q - p = {0}", q - p);
37 ]]></code_example>which produces the output: <code_example><![CDATA[
40 ]]></code_example>end example]</example>
42 <paragraph>If a pointer arithmetic operation overflows the domain of the pointer type, the result is truncated in an implementation-defined fashion, but no exceptions are produced. </paragraph>