简体   繁体   English

指针和数组的C / C ++ Char指针

[英]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: 首先,帮助解码函数的一些经验法则:

  • the type is really equivalent to a char*** . 这种类型实际上相当于一个char*** This means we have to find a meaning for three pointers. 这意味着我们必须找到三个指针的含义。
  • a string in C is a char* (leaving out the const for brevity). C中的字符串是char* (为简洁起见省略了const )。 As we expect the function to populate an array of strings, that's one * accounted for. 正如我们所期望的那样,函数填充了一个字符串数组,这就是一个*
  • an array in C is generally represented by a pointer. C中的数组通常由指针表示。 So that's the second * . 那是第二个*
  • "output parameters" can be handled in one of two ways: either the caller allocates memory, and passes a pointer, telling callee where to write its data, or the caller does not allocate any memory, and simply passes in a pointer to a pointer. “输出参数”可以通过以下两种方式中的一种来处理:或者呼叫者分配内存,并传递一个指针,告诉被叫方在何处写入其数据,或呼叫者分配任何存储器中,并且简单地通过在一个指针的指针。 The callee will then modify the "innermost" pointer to point to a chunk of memory allocated by the callee itself. 然后被调用者将修改“最里面”指针以指向被调用者自己分配的一块内存。 The second approach would account for the last * . 第二种方法将解释最后的* 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM