ECMA-334 C# Language Specification25.4: Pointer conversions |
In an unsafe context, the set of available implicit conversions (13.1) is extended to include the following implicit pointer conversions:
pointer-type
to the type void*. pointer-type
. Additionally, in an unsafe context, the set of available explicit conversions (13.2) is extended to include the following explicit pointer conversions:
pointer-type
to any other pointer-type
. pointer-type
. pointer-type
to sbyte , byte , short , ushort , int , uint , long , or ulong . Finally, in an unsafe context, the set of standard implicit conversions (13.3.1) includes the following pointer conversion:
pointer-type
to the type void*. Conversions between two pointer types never change the actual pointer value. In other words, a conversion from one pointer type to another has no effect on the underlying address given by the pointer.
When one pointer type is converted to another, if the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined if the result is dereferenced. In general, the concept "correctly aligned" is transitive: if a pointer to type A is correctly aligned for a pointer to type B, which, in turn, is correctly aligned for a pointer to type C, then a pointer to type A is correctly aligned for a pointer to type C.
end example]
char c = 'A';
char* pc = &c;
void* pv = pc;
int* pi = (int*)pv;
int i = *pi; // undefined
*pi = 123456; // undefined
When a pointer type is converted to a pointer to byte , the result points to the lowest addressed byte of the variable. Successive increments of the result, up to the size of the variable, yield pointers to the remaining bytes of that variable.
using System;
class Test
{
static void Main() {
double d = 123.456e23;
unsafe {
byte* pb = (byte*)&d;
for (int i = 0; i < sizeof(double); ++i)
Console.Write(" {0,2:X}", (uint)(*pb++));
Console.WriteLine();
}
}
}
Mappings between pointers and integers are implementation-defined.