如何在c++中声明byte*(字节数组)

How to Declare byte* ( byte array ) in c++?

本文关键字:字节 字节数 数组 byte c++ 声明      更新时间:2023-10-16

如何在c++中声明字节*(字节数组)以及如何在函数定义中定义为参数?

当我在

下面声明like时

函数声明:

int Analysis(byte* InputImage,int nHeight,int nWidth);

获取错误:"byte" undefined

c++中没有byte类型。你之前应该用typedef。就像

typedef std::uint8_t byte;
c++ 11中的

,或者

typedef unsigned char byte;
在c++ 03。

表示字节的c++类型是unsigned char(或char的其他符号风格,但如果您希望它作为纯字节,unsigned可能是您所追求的)。

然而,在现代c++中,您不应该使用原始数组。如果数组是运行时大小,使用std::vector<unsigned char>;如果数组是静态大小N,使用std::array<unsigned char, N> (c++ 11)。您可以通过(const)引用将它们传递给函数,如下所示:

int Analysis(std::vector<unsigned char> &InputImage, int nHeight, int nWidth);

如果Analysis不修改数组或其元素,则执行以下操作:

int Analysis(const std::vector<unsigned char> &InputImage, int nHeight, int nWidth);