【问题标题】:Can you please help explain this code?你能帮忙解释一下这段代码吗?
【发布时间】:2011-06-09 09:54:00
【问题描述】:

我有这个函数,我正在尝试转换,但我无法理解代码的某些部分发生了什么。谁能帮我解释一下代码。我只想知道他们对指针做了什么。代码中有一些空白的 cmets 用指针做地狱,我就是不明白。

任何帮助表示赞赏。

WORD** m_Pixels;

int pixel(int x, int y)
{

    if (x<0 || y<0 || x>=m_Width || y>=m_Height)
        return -1;

    WORD    *pPixels = m_Pixels[y];

    //
    int count = *pPixels++;

    int index = 0;

    register int i;

    if (count > 0)
    {
        i = count;
        do {
            // 
            index += *pPixels++;

            if (x < index)
            {
                return -1;
            }

            //      
            index += *pPixels;

            // 
            pPixels += *pPixels;

            pPixels++;


            // 
            index += *pPixels;

            // 
            pPixels += *pPixels;

            pPixels++;

            if (x < index)
            {
                return pPixels[x-index];
            }
        } while (--i);
    }

    return -1;
}

【问题讨论】:

  • 你从哪里得到这个代码,你不明白的部分是什么。
  • 我只是不明白他们在做什么,pPixels++;,他们在增加什么?
  • 哇,那是一些非常难看的代码
  • @Michael I just dont understand what they do。他们只是指针。 :)
  • 看起来您的 m_Pixels 是每个 y 的(标题,数据)部分的列表,它正在遍历结构,直到找到其中包含第 x 个像素的部分。 pPixels++ 可能会跳过数据头中的值到他们想要的计数。有人可以坐下来为你转换这段代码,是的,但是你理解和转换它正在读取的数据结构会更有意义(假设你自己也写它 - 或者它是你正在读取的文件? )

标签: c++ pointers pointer-arithmetic


【解决方案1】:
int count = *pPixels++;

取消引用 pPixels 指针以获取值并将其分配给 count 并递增指针 - 这将使指针指向数组中的下一个元素 (m_Pixels)


index += *pPixels++;

使用pPixels 指向的值增加index 并增加指针 - 这将使指针指向数组中的下一个元素


pPixels += *pPixels;
pPixels += *pPixels;

将指针 X 向前移动,其中 X 是值,由pPixels 指向

【讨论】:

  • awesome mate =) 我只是想验证一下,一切似乎都是正确的。谢谢!
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 2018-11-03
相关资源
最近更新 更多