13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 25 Unsafe codeend example]using System;class Test{static void Main() {Point point;unsafe {Point* p = &point;(*p).x = 10;(*p).y = 20;Console.WriteLine((*p).ToString());}}}25.5.3 Pointer element accessA pointer-element-access consists of a primary-no-array-creation-expression followed by an expressionenclosed in “[” and “]”.pointer-element-access:primary-no-array-creation-expression [ expression ]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.A pointer element access of the form P[E] is evaluated exactly as *(P + E). For a description of the pointerindirection operator (*), see §25.5.1. For a description of the pointer addition operator (+), see §25.5.6.[Example: In the exampleclass 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]The pointer element access operator does not check for out-of-bounds errors and the behavior whenaccessing an out-of-bounds element is undefined. [Note: This is the same as C and C++. end note]25.5.4 The address-of operatorAn addressof-expression consists of an ampersand (&) followed by a unary-expression.addressof-expression:& unary-expressionGiven an expression E which is of a type T and is classified as a fixed variable (§25.3), the construct &Ecomputes the address of the variable given by E. The type of the result is T* and is classified as a value. A325

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!