首页 > 解决方案 > 指向全局变量的指针

问题描述

所以,我有这样的代码(详细):

namespace itssafe
{
    public unsafe partial class testing : Form
    {
        unsafe private static int original = 10;
        unsafe private int tmp;

        unsafe setter() // I'll use this several time
        {
            tmp = original;
        }

        unsafe private void Form1_Load(object sender, EventArgs e)
        {
            setter();
        }

        unsafe private void timers_Tick(object sender, EventArgs e)
        {
            int* counter;   // working
            counter = &tmp; // not working, but I need this
        }
    }
}

我有一个名为“tmp”的全局变量,我需要创建一个指向它的指针。但我不能,因为我得到“ CS0212 You can only take the address of an unfixed expression inside of a fixed statement initializer ”错误。但是这个变量是全局的,所以我不能用这个方法创建,因为我也需要在其他地方使用。我能做些什么?

我知道我已经习惯了许多不必要的“不安全”,但我很绝望。

标签: c#winformsunsafe-pointers

解决方案


推荐阅读