Accessing Structure Members with a Pointer : Unsafe Code « Language Basics « C# / C Sharp






Accessing Structure Members with a Pointer

 
using System;
   
public struct Point2D {
    public int X;
    public int Y;
}
   
public class MyClass {
    public unsafe static void Main() {
        Point2D MyPoint;
        Point2D * PointerToMyPoint;
   
        MyPoint = new Point2D();
        PointerToMyPoint = &MyPoint;
        PointerToMyPoint->X = 100;
        PointerToMyPoint->Y = 200;
        Console.WriteLine("({0}, {1})", PointerToMyPoint->X, PointerToMyPoint->Y);
    }
}
           
         
  








Related examples in the same category

1.int pointer variable
2.Unsafe Methods
3.Supported sizeof() Types
4.Fixing Managed Data in Memory
5.Allocating Memory from the Stack
6.Get variable address in unsafe mode
7.Unsafe code: get data type size
8.Address and size of pointer object
9.Using the unsafe keyword
10.object pointer
11.Use unsafe method to clone array
12.unsafe and fixed block
13.mark method as unsafe
14.Use unsage code to swap two integers
15.Pointer for struct
16.Compare pointer