Jon Jagger
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 25 Unsafe codeprevious at this levelnext at this level 25.5 Pointers in expressionsprevious at this levelnext at this level 25.5.4 The address-of operator Paragraph 11 An addressof-expression consists of an ampersand (&) followed by a unary-expression. addressof-expression : & unary-expression Paragraph 21 Given an expression E which is of a type T and is classified as a fixed variable (§25.3), the construct &E computes the address of the variable given by E. 2 The type of the result is T* and is classified as a value. 3 A compile-time error occurs if E is not classified as a variable, if E is classified as a volatile field (§17.4.3), or if E denotes a moveable variable. 4 In the last case, a fixed statement (§25.6) can be used to temporarily "fix" the variable before obtaining its address. Paragraph 31 The & operator does not require its argument to be definitely assigned, but following an & operation, the variable to which the operator is applied is considered definitely assigned in the execution path in which the operation occurs. 2 It is the responsibility of the programmer to ensure that correct initialization of the variable actually does take place in this situation. [Example: In the example
using System;  
class Test  
   static void Main() {  
      int i;  
      unsafe {  
         int* p = &i;  
         *p = 123;  
i is considered definitely assigned following the &i operation used to initialize p. The assignment to *p in effect initializes i, but the inclusion of this initialization is the responsibility of the programmer, and no compile-time error would occur if the assignment was removed. end example]
[Note: The rules of definite assignment for the & operator exist such that redundant initialization of local variables can be avoided. For example, many external APIs take a pointer to a structure which is filled in by the API. Calls to such APIs typically pass the address of a local struct variable, and without the rule, redundant initialization of the struct variable would be required. end note] [Note: As stated in §14.5.4, outside an instance constructor or static constructor for a struct or class that defines a readonly field, that field is considered a value, not a variable. As such, its address cannot be taken. Similarly, the address of a constant cannot be taken. end note]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS