Question

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**. 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!

Was it helpful?

Solution

char ***element[size];

Is not exactly a 3D array, but an array of size elements of pointers-to-pointers-to-pointers to char.

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.

On further reading, it appears that the parameter is not really a 3D array but a pointer to an array of C-style strings. Note, the syntax may be the same, the intent is different.

In that case, you'll need to do two things:

  • Specify the number of strings you want to store in the array
  • For each string
    • Allocate memory
    • Copy string data to the char array

And finally, you'll be passing in the address of this array of strings on to the credis_lrange function.

OTHER TIPS

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).

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top