[英]C / C++ Char Pointers to Pointers and Arrays
There is a function with a header such as this: 有一个带有标题的函数,例如:
BPS_API int dialog_event_get_filebrowse_filepaths(bps_event_t* event,
char** file_paths[], int* num_paths);
This is from BlackBerry 10's Native SDK for anyone wondering (it can be found here ). 这是来自BlackBerry 10的Native SDK,适合任何想知道的人(可以在这里找到)。
The question is: what am I supposed to provide as the second argument. 问题是:我应该提供什么作为第二个论点。 This function should populate an array of char pointers in order to return the file paths selected.
此函数应填充一个char指针数组,以便返回所选的文件路径。
I tried to call it this way: 我试着这样称呼它:
char* ar[2];
dialog_event_get_filebrowse_filepaths(event, &ar, &number_paths);
And I am getting an error from QNX Momentics as such: 我从QNX Momentics那里收到错误:
cannot convert 'char * (*)[2]' to char * * * for argument 2 to int
dialog_event_get_filebrowse_filepaths(bps_event_t *, char * * *, int *)
This seems the most logical way to call it. 这似乎是最合乎逻辑的称呼方式。 It needs the memory address of an array of pointer in order to set them, as far as I understand.
据我所知,它需要一个指针数组的内存地址才能设置它们。 However, if I declare:
但是,如果我声明:
char** ar[2];
dialog_event_get_filebrowse_filepaths(event, ar, &number_paths);
It works, but this way I created an array of pointers to char pointers (an array of arrays of char*). 它工作,但这样我创建了一个指向char指针的数组(char *数组的数组)。 Is this what I should really provide to the function?
这是我应该真正提供的功能吗?
The documentation really should have been more explicit, but I'm pretty sure this is what the function expects: 文档应该更加明确,但我很确定这是函数所期望的:
First, a few rules of thumb to help decode the function: 首先,帮助解码函数的一些经验法则:
char***
. char***
。 This means we have to find a meaning for three pointers. char*
(leaving out the const
for brevity). char*
(为简洁起见省略了const
)。 As we expect the function to populate an array of strings, that's one *
accounted for. *
。 *
. *
。 *
. *
。 This also explains the documentation note that The memory holding these values must be freed using bps_free() when no longer needed
-- because the memory was allocated by the library function, rather than by you, it is important that you use the correct free
function. The memory holding these values must be freed using bps_free() when no longer needed
的内存 - 因为内存是由库函数分配的,而不是由你来分配,所以使用正确的free
函数很重要。 So, the function should be called like this: 因此,应该像这样调用函数:
char** strs;
dialog_event_get_filebrowse_filepaths(foo, &strs, bar);
When the function returns, strs
will point to the first entry of an array of strings allocated by dialog_event_get_filebrowse_filepaths
. 当函数返回时,
strs
将指向由dialog_event_get_filebrowse_filepaths
分配的字符串数组的第一个条目。
Just a guess, but I suspect it should be called like this: 只是一个猜测,但我怀疑它应该像这样调用:
char** ar = 0;
dialog_event_get_filebrowse_filepaths(event, &ar, &number_paths);
The fact that it is declared with array syntax is confusing, but this would be a common way for a C-style function to return an allocated array of strings. 使用数组语法声明它的事实令人困惑,但这将是C样式函数返回已分配的字符串数组的常用方法。
the function will return a string-array and the length via arguments, you can use it like: 该函数将通过参数返回一个字符串数组和长度,您可以使用它:
char **ar=0;
int i,number_paths = 0;
dialog_event_get_filebrowse_filepaths(event, &ar, &number_paths);
for( i=0; i<number_paths; i++ )
puts( ar[i] );
and you can/must free the memory ( see lib-docu ) like: 你可以/必须释放内存(参见lib-docu),如:
for( i=0; i<number_paths; i++ )
free( ar[i] );
free( ar );
Here is some (non-Blackberry) code that should satisfy the compiler: 这是一些应该满足编译器的(非Blackberry)代码:
char *file_paths[];
iret = dialog_event_get_filebrowse_filepaths (event, &ar, &number_paths);
The point is 重点是
1) do not assign a fixed length in the declaration. 1) 不分配在声明一个固定的长度。
2) pass addressof (&) array pointer to your function 2)将(&)数组指针的地址传递给您的函数
3) make sure somebody (either the API function, or you) allocates the array, and all the strings in the array 3)确保某人 (无论是API函数还是你)分配数组,以及数组中的所有字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.