Export (0) Print
Expand All

A.5.2 Pointer member access

Visual Studio .NET 2003

A pointer-member-access consists of a primary-expression, followed by a "->" token, followed by an identifier.

pointer-member-access:
primary-expression   ->   identifier

In 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 pointer indirection operator (*), see Section A.5.1. For a description of the member access operator (.), see Section 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());
      }
   }
}
Show:
© 2014 Microsoft