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.

<strong>C#</strong> LANGUAGE SPECIFICATION25.5.1 Pointer indirectionA pointer-indirection-expression consists of an asterisk (*) followed by a unary-expression.pointer-indirection-expression:* unary-expressionThe unary * operator denotes pointer indirection and is used to obtain the variable to which a pointer points.The result of evaluating *P, where P is an expression of a pointer type T*, is a variable of type T. It is acompile-time error to apply the unary * operator to an expression of type void* or to an expression thatisn’t of a pointer type.The effect of applying the unary * operator to a null pointer is implementation-defined. In particular, thereis no guarantee that this operation throws a System.NullReferenceException.If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined. [Note:Among the invalid values for dereferencing a pointer by the unary * operator are an address inappropriatelyaligned for the type pointed to (see example in §25.4), and the address of a variable after the end of itslifetime. end note]For purposes of definite assignment analysis, a variable produced by evaluating an expression of the form*P is considered initially assigned (§12.3.1).25.5.2 Pointer member accessA pointer-member-access consists of a primary-expression, followed by a “->” token, followed by anidentifier.pointer-member-access:primary-expression -> identifierIn a pointer member access of the form P->I, P must be an expression of a pointer type other than void*,and I must denote an accessible member of the type to which P points.A pointer member access of the form P->I is evaluated exactly as (*P).I. For a description of the pointerindirection operator (*), see §25.5.1. For a description of the member access operator (.), see §14.5.4.[Example: In the examplestruct Point{public int x;public int y;public override string ToString() {return "(" + x + "," + y + ")";}}using System;class Test{static void Main() {Point point;unsafe {Point* p = &point;p->x = 10;p->y = 20;Console.WriteLine(p->ToString());}}}the -> operator is used to access fields and invoke a method of a struct through a pointer. Because theoperation P->I is precisely equivalent to (*P).I, the Main method could equally well have been written:324

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

Saved successfully!

Ooh no, something went wrong!