
25 Unsafe code
25.5 Pointers in expressions
25.5.3 Pointer element access
Paragraph 11 A pointer-element-access consists of a primary-no-array-creation-expression followed by an expression enclosed in "[" and "]". pointer-element-access : primary-no-array-creation-expression [ expression ]
Paragraph 21 In a pointer element access of the form P[E], P must be an expression of a pointer type other than void*, and E must be an expression of a type that can be implicitly converted to int, uint, long, or ulong.
Paragraph 31 A pointer element access of the form P[E] is evaluated exactly as *(P + E). 2 For a description of the pointer indirection operator (*), see §25.5.1. 3 For a description of the pointer addition operator (+), see §25.5.6. [Example: In the example
class Test
{
static void Main() {
unsafe {
char* p = stackalloc char[256];
for (int i = 0; i < 256; i++) p[i] = (char)i;
}
}
}
a pointer element access is used to initialize the character buffer in a for loop. Because the operation P[E] is precisely equivalent to *(P + E), the example could equally well have been written:
class Test
{
static void Main() {
unsafe {
char* p = stackalloc char[256];
for (int i = 0; i < 256; i++) *(p + i) = (char)i;
}
}
}
end example]
Paragraph 41 The pointer element access operator does not check for out-of-bounds errors and the behavior when accessing an out-of-bounds element is undefined. [Note: This is the same as C and C++. end note]
| |
| Jagger Software Ltd | |
| Company # 4070126 | |
| VAT # 762 5213 42 |