gpt4 book ai didi

指向数组指针的 C++ 指针

转载 作者:行者123 更新时间:2023-12-04 23:10:59 25 4
gpt4 key购买 nike

如何将数组传递给这个函数?
这是功能:

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);
为什么我需要 *pointertoX ?
有没有其他方法可以做到这一点?
整个代码:
#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

最佳答案

this is the function :

void fire(const uint8_t *const s[])

该函数接受指向 const 的指针。指向 const uint8_t 的指针.

and I want to pass this array to that :

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

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

why I need *pointertoX ?


因为该函数接受指向 const 的指针。指向 const uint8_t 的指针, 和 &pointertoX是指向 unsigned char 的指针的指针.鉴于 uint8_tunsigned char 的别名, &pointertoX可隐式转换为函数参数。

note : I'm trying to pass bitmap to ffmpeg "sws_scale" ..


仔细阅读文档:

srcSlice the array containing the pointers to the planes of the source slice

dst the array containing the pointers to the planes of the destination image



您正在尝试将一个字符数组传递给一个需要指针数组的函数。

附言程序的行为未定义,因为 *s不指向空终止的字符串,但您将其插入到具有此类要求的字符流中。

关于指向数组指针的 C++ 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68855382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com