1

我在代码中发现了以下我不完全理解的语句:

UInt32 *pixels;
UInt32 *currentPixel = pixels;
UInt32 color = *currentPixel;

前两行对我来说很清楚,因为它们是 UInt32 对象、像素和 currentPixel 的定义。但老实说,后面的行对我来说没有意义。为什么不是:

UInt32 *color = currentPixel

UInt32 color = *currentPixel

那有什么区别呢?

如果我从 currentPixel 中删除 *,我会收到消息: Incompatible pointer to integer conversion initializing 'UInt32' (aka 'unsigned int') with an expression of type 'UInt32 *' (aka 'unsigned int *'); 取消引用 *

带 * 的取消引用是什么意思?

谢谢

4

1 回答 1

2
// alloc height * width * 32 bit memory. pixels is first address.
UInt32 *pixels = (UInt32 *) calloc(height * width, sizeof(UInt32));

// you can do like this
UInt32 color = pixels[3]

// or like this, they are equal.
UInt32 color = *(pixels + 3)

有时像数组一样的指针。

有一个关于指针的教程: http ://www.cplusplus.com/doc/tutorial/pointers/

UInt32 不是对象。它在 32 位机器中是无符号长的。64位机器中的无符号整数。

有它的定义:

#if __LP64__
typedef unsigned int                    UInt32;
typedef signed int                      SInt32;
#else
typedef unsigned long                   UInt32;
typedef signed long                     SInt32;
#endif
于 2015-02-26T21:46:18.217 回答