Pointer arithmetic

suggest change

Addition and subtraction in pointers works differently from integers. When a pointer is incremented or decremented, the address it points to is increased or decreased by the size of the referent type.

For example, the type int (alias for System.Int32) has a size of 4. If an int can be stored in address 0, the subsequent int can be stored in address 4, and so on. In code:

var ptr = (int*)IntPtr.Zero;
Console.WriteLine(new IntPtr(ptr)); // prints 0
ptr++;
Console.WriteLine(new IntPtr(ptr)); // prints 4
ptr++;
Console.WriteLine(new IntPtr(ptr)); // prints 8

Similarly, the type long (alias for System.Int64) has a size of 8. If a long can be stored in address 0, the subsequent longcan be stored in address 8, and so on. In code:

var ptr = (long*)IntPtr.Zero;
Console.WriteLine(new IntPtr(ptr)); // prints 0
ptr++;
Console.WriteLine(new IntPtr(ptr)); // prints 8
ptr++;
Console.WriteLine(new IntPtr(ptr)); // prints 16

The type void is special and void pointers are also special and they are used as catch-all pointers when the type isn’t known or doesn’t matter. Due to their size-agnostic nature, void pointers cannot be incremented or decremented:

var ptr = (void*)IntPtr.Zero;
Console.WriteLine(new IntPtr(ptr));
ptr++; // compile-time error
Console.WriteLine(new IntPtr(ptr));
ptr++; // compile-time error
Console.WriteLine(new IntPtr(ptr));

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Pointers:
* Pointer arithmetic
* void

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script