首页 > Windows开发 > 详细

(C#)获取变量的地址

时间:2020-11-12 10:38:30      阅读:32      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Text;

namespace AddressOfVariable
{
    class Program
    {
        class Point
        {
            public int x;
            public int y;
        }

        static void Main(string[] args)
        {
            int number;
            //获取栈上变量的地址
            unsafe
            {
                int* p = &number;
                *p = 0xffff;
                Console.WriteLine("{0:x}", *p);
                Console.WriteLine("Address of numbe:0x{0:x}", (int)p);
            }

            string message = "Hello World!";
            //获取堆上变量的地址
            unsafe
            {
                fixed (char* p = message)
                {
                    Console.WriteLine("Address of message 0x{0:x}", (int)p);
                }
            }

            Point point = new Point();
            //获取堆上变量的地址
            unsafe
            {
                fixed (int* p = &point.x)
                {
                    Console.WriteLine("Address of point 0x{0:x}", (int)p);
                }
            }                
        }
    }
}

(C#)获取变量的地址

原文:https://www.cnblogs.com/Micah-blog/p/13962086.html

(0)
(0)
   
举报
评论 一句话评论(0
分享档案
最新文章
教程昨日排行
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!