31 lines
2.2 KiB
XML
Raw Normal View History

<?xml version="1.0"?>
<clause number="25.5.3" title="Pointer element access">
<paragraph>A <non_terminal where="25.5.3">pointer-element-access</non_terminal> consists of a <non_terminal where="14.5">primary-no-array-creation-expression</non_terminal> followed by an expression enclosed in &quot;[&quot; and &quot;]&quot;. <grammar_production><name><non_terminal where="25.5.3">pointer-element-access</non_terminal></name> : <rhs><non_terminal where="14.5">primary-no-array-creation-expression</non_terminal><terminal>[</terminal><non_terminal where="14.14">expression</non_terminal><terminal>]</terminal></rhs></grammar_production></paragraph>
<paragraph>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 <keyword>int</keyword>, <keyword>uint</keyword>, <keyword>long</keyword>, or <keyword>ulong</keyword>. </paragraph>
<paragraph>A pointer element access of the form P[E] is evaluated exactly as *(P + E). For a description of the pointer indirection operator (*), see <hyperlink>25.5.1</hyperlink>. For a description of the pointer addition operator (+), see <hyperlink>25.5.6</hyperlink>. </paragraph>
<paragraph>
<example>[Example: In the example <code_example><![CDATA[
class Test
{
static void Main() {
unsafe {
char* p = stackalloc char[256];
for (int i = 0; i < 256; i++) p[i] = (char)i;
}
}
}
]]></code_example>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: <code_example><![CDATA[
class Test
{
static void Main() {
unsafe {
char* p = stackalloc char[256];
for (int i = 0; i < 256; i++) *(p + i) = (char)i;
}
}
}
]]></code_example>end example]</example>
</paragraph>
<paragraph>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>[Note: This is the same as C and C++. end note]</note> </paragraph>
</clause>