[英]C++ How to use and pass a 3-dimensional char array?
I'm trying to build a char array for storing the return value of a function. In the following function the data is stored in *****valv**.我正在尝试构建一个 char 数组来存储 function 的返回值。在以下 function 中,数据存储在 *****valv** 中。 How to build a extern variable to access the data?如何构建外部变量来访问数据?
int credis_lrange(REDIS rhnd, const char *key,
int start, int end, char ***valv)
{
int rc;
if ((rc = cr_sendfandreceive(rhnd, CR_MULTIBULK, "LRANGE %s %d %d\r\n",
key, start, end)) == 0)
{
*valv = rhnd->reply.multibulk.bulks;
rc = rhnd->reply.multibulk.len;
}
return rc;
}
Solution:解决方案:
char **elements;
int size = credis_lrange(this->redis,"object_2",600,603,&elements);
for (int i=0; i<size;i++) {
cout << "element: " << elements[i] << endl;
}
Thanks to everyone!谢谢大家!
char ***element[size];
Is not exactly a 3D array, but an array of size
elements of pointers-to-pointers-to-pointers to char
. 并非完全是3D数组,而是char
的指针到指针到指针的size
元素数组。
Use any one of the following: 使用以下任何一项:
char e[ D1 ][ D2 ][ D3 ]; /* D1, D2, D3 are integral constants */
char *e[ D2 ][ D3 ];
char e[][ D2 ][ D3 ];
Also, you can pass it on by simply speficying e
as the argument to your function. 同样,您可以通过简单地将e
作为函数的参数来传递它。
On further reading, it appears that the parameter is not really a 3D array but a pointer to an array of C-style strings. 进一步阅读后,该参数实际上并不是3D数组,而是指向C样式字符串数组的指针。 Note, the syntax may be the same, the intent is different. 注意,语法可能相同,但意图不同。
In that case, you'll need to do two things: 在这种情况下,您需要做两件事:
char
array 将字符串数据复制到char
数组 And finally, you'll be passing in the address of this array of strings on to the credis_lrange
function. 最后,您将把这个字符串数组的地址传递给credis_lrange
函数。
I only found one hit on Google for this, but it looks like the cr_sendfandreceive function allocates its rhnd->reply.multibulk.bulks member, so you don't actually have to pass it back (since you were passed rhnd in the first place). 我为此只在Google上找到了一个热门产品,但是看起来cr_sendfandreceive函数分配了其rhnd-> reply.multibulk.bulks成员,因此您实际上不必将其传递回来(因为首先传递了rhnd) )。
If you want to copy it, then you would declare elements as a char** and pass its address (or use references), and then inside the method you would clone the bulks member and also each string in the array (in a loop). 如果要复制它,则可以将元素声明为char **并传递其地址(或使用引用),然后在该方法内部克隆出大块成员以及数组中的每个字符串(循环)。 。
In Arduino Studio environment, for MCUs programming like ST or ESP32 using PSRAM, one can allocate dynamic memory for a 3D multiarray of char array, sized 255, like so:在 Arduino Studio 环境中,对于使用 PSRAM 进行 ST 或 ESP32 等 MCU 编程,可以为 3D 字符数组的多数组分配动态 memory,大小为 255,如下所示:
char*** 3d_array = (char***) heap_caps_malloc( sizeof(char)*255*(size_x*size_y*size_z), MALLOC_CAP_SPIRAM);
for X86/ X64 architectures a 3D multiaaray of char array, sized 255 can be declared as follows:对于 X86/X64 架构,一个 3D 字符数组的多数组,大小为 255 可以声明如下:
char*** 3d_array = (char***) malloc( sizeof(char)*255*(size_x*size_y*size_z));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.