在一条语句中取消引用和前进指针? [英] dereference and advance pointer in one statement?

查看:86
本文介绍了在一条语句中取消引用和前进指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从字节数组中读取内容,如下所示:

I'm reading from a byte array as follows:

int* i = (int*)p;
id = *i;
i++;

如果我错了,请纠正我,但是++的优先级高于*,因此可以在同一条语句中组合* i和i ++吗? (例如* i ++)

correct me if I'm wrong, but ++ has precedence over *, so is possible to combine the *i and i++ in the same statement? (e.g. *i++)

(从技术上讲这是不安全的C#,不是C ++,p是字节*)

(this is technically unsafe C#, not C++, p is a byte*)

推荐答案

我相信

id = *i;
i++;

id = *i++;

等效.

++运算符用作后缀(例如i++)时,将返回递增之前的变量值.

The ++ operator, when used as a suffix (e.g. i++), returns the value of the variable prior to the increment.

我对于

unsafe class Test
{
    static public void Test1(int p, out int id)
    {
        int* i = (int*)(p);
        id = *i;
        i++;
    }

    static public void Test2(int p, out int id)
    {
        int* i = (int*)(p);
        id = *i++;
    }
}

显示为

public static unsafe void Test1(int p, out int id)
{
    int* i = (int*) p;
    id = i[0];
    i++;
}

public static unsafe void Test2(int p, out int id)
{
    int* i = (int*) p;
    i++;
    id = i[0];
}

这显然不是等效的.

这篇关于在一条语句中取消引用和前进指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆