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

Pix*_*xel 3 c++ arrays

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

当我像下面这样声明时

函数声明:

int Analysis(byte* InputImage,int nHeight,int nWidth);
Run Code Online (Sandbox Code Playgroud)

出现错误:“字节”未定义

For*_*veR 6

C++ 中没有类型byte。你应该typedef先用。就像是

typedef std::uint8_t byte;
Run Code Online (Sandbox Code Playgroud)

在 C++11 中,或

typedef unsigned char byte;
Run Code Online (Sandbox Code Playgroud)

在C++03中。


Rei*_*ica 5

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

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

int Analysis(std::vector<unsigned char> &InputImage, int nHeight, int nWidth);
Run Code Online (Sandbox Code Playgroud)

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

int Analysis(const std::vector<unsigned char> &InputImage, int nHeight, int nWidth);
Run Code Online (Sandbox Code Playgroud)