指向数组指针的c++指针



如何将数组传递给这个函数?

这是函数:

void fire(const uint8_t *const s[])
{
cout<<*s<<endl;
}

我想把这个数组传递给它:

unsigned char X[10] = {255,255,255,255};

它是由这个完成的,它工作

unsigned char X[5] = {255,255,255,255};
unsigned char *pointertoX ;
pointertoX = X;
fire(&pointertoX);

为什么我需要* poinintertox ?还有其他方法吗?

完整代码:

#include <iostream>
using namespace std;

void fire(const uint8_t *const s[])
{
cout<<*s<<endl;
}

int main() {

unsigned char X[10] = {255,255,255,255};
unsigned char *pointertoX ;
pointertoX = X;
fire(&pointertoX);

return 0;
}

注意:我试图将位图传递给ffmpeg "sws_scale". .

https://ffmpeg.org/doxygen/4.1/group__libsws.html gae531c9754c9205d90ad6800015046d74

这是函数:

void fire(const uint8_t *const s[])

该函数接受一个指向const的指针,指向const uint8_t的指针

我想把这个数组传递给它:

unsigned char X[10] = {255,255,255,255};

你不能。

为了将数组传递给接受指针的函数,该函数必须接受指向该数组元素类型的指针(在其他隐式转换之后,例如从非const指针转换为指向const指针)。该数组的元素是unsigned char,而该函数接受指向const的指针,指向const uint8_t

为什么我需要*pointertoX ?

因为函数接受指向const的指针指向const uint8_t,而&pointertoX是指向unsigned char的指针。由于uint8_tunsigned char的别名,因此&pointertoX可以隐式转换为函数形参。


注意:我正在尝试将位图传递给ffmpeg "sws_scale". .

仔细阅读文档:

srcSlice数组包含指针到源切片的平面

dst包含指针的数组到目标图像的平面

你试图将一个字符数组传递给一个需要指针数组的函数。


注:程序的行为是未定义的,因为*s没有指向一个以空结束的字符串,但是你将它插入到一个有这样要求的字符流中。

最新更新