X hits on this document

1134 views

0 shares

0 downloads

0 comments

336 / 396

Chapter ‎18   Unsafe code

A pointer member access of the form P->I is evaluated exactly as (*P).I. For a description of the pointer indirection operator (*), see §‎18.5.1. For a description of the member access operator (.), see §‎7.5.4.

In the example

using System;

struct Point { public int x; public int y;

public override string ToString() { return "(" + x + "," + y + ")"; } }

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 the operation P->I is precisely equivalent to (*P).I, the Main method could equally well have been written:

class Test { static void Main() { Point point; unsafe { Point* p = &point; (*p).x = 10; (*p).y = 20; Console.WriteLine((*p).ToString()); } } }

18.5.3 Pointer element access

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   ]

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 pointer indirection operator (*), see §‎18.5.1. For a description of the pointer addition operator (+), see §‎18.5.6.

In the example

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.323

Document info
Document views1134
Page views1134
Page last viewedTue Jan 17 13:41:04 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments