Get variable address in unsafe mode : Unsafe Code « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Language Basics » Unsafe Code 




Get variable address in unsafe mode
  

using System;

public class MainEntryPoint {
    public static unsafe void Main() {
        Console.WriteLine("Size of Currency struct is " + sizeof(MyStruct));
        MyStruct amount1, amount2;
        MyStruct* pointerStruct = &amount1;
        long* pDollars = &(pointerStruct->Dollars);
        byte* pCents = &(pointerStruct->Cents);

        Console.WriteLine("Address of amount1 is 0x{0:X}"(uint)&amount1);
        Console.WriteLine("Address of amount2 is 0x{0:X}"(uint)&amount2);
        Console.WriteLine("Address of pAmt is 0x{0:X}"(uint)&pointerStruct);
        Console.WriteLine("Address of pDollars is 0x{0:X}"(uint)&pDollars);
        Console.WriteLine("Address of pCents is 0x{0:X}"(uint)&pCents);
        pointerStruct->Dollars = 20;
        *pCents = 50;
        Console.WriteLine("amount1 contains " + amount1);
        --pointerStruct;   
        Console.WriteLine("amount2 has address 0x{0:X} and contains {1}",
           (uint)pointerStruct, *pointerStruct);
        MyStruct* pTempCurrency = (MyStruct*)pCents;
        pCents = (byte*)(--pTempCurrency);
        Console.WriteLine("Address of pCents is now 0x{0:X}"(uint)&pCents);
        Console.WriteLine("\nNow with classes");

        MyClass amount3 = new MyClass();

        fixed (long* pDollars2 = &(amount3.Dollars))
        fixed (byte* pCents2 = &(amount3.Cents)) {
            Console.WriteLine("amount3.Dollars has address 0x{0:X}"(uint)pDollars2);
            Console.WriteLine("amount3.Cents has address 0x{0:X}"(uint)pCents2);
            *pDollars2 = -100;
            Console.WriteLine("amount3 contains " + amount3);
        }
    }
}

struct MyStruct {
    public long Dollars;
    public byte Cents;

    public override string ToString() {
        return "$" + Dollars + "." + Cents;
    }
}

class MyClass {
    public long Dollars;
    public byte Cents;

    public override string ToString() {
        return "$" + Dollars + "." + Cents;
    }
}

   
  














Related examples in the same category
1.int pointer variable
2.Unsafe Methods
3.Accessing Structure Members with a Pointer
4.Supported sizeof() Types
5.Fixing Managed Data in Memory
6.Allocating Memory from the Stack
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.