指向全局变量的指针

时间:2018-09-26 00:25:08

标签: c# winforms unsafe-pointers

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

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,您只能在固定语句初始化程序内获取未固定表达式的地址”错误。但是变量是全局变量,因此我无法在此方法中创建,因为我也需要在其他地方使用。我该怎么办?

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

0 个答案:

没有答案
相关问题