0

I am trying to use the function rrd_update_r of the round robin database.

int       rrd_update_r(const char *filename,const char *_template,
                                   int argc,const char **argv);

The function accepts the as 3rd and 4th argument the well known argc, argv.

Even though I am using C++ (and g++) for this project, rrd is written in C and consequently I could use the function wordexp(char *, wordexp_t*) provided in GNY/Linux to split the arguments of a string into an argv array.

The problem is that wordexp_t returns a member of char ** type (as argv), which is incompatible apparently with the rrd_update_r function call.

/usr/include/rrd.h:238:15: error:   initializing argument 4 of ‘int rrd_update_r(const char*, const char*, int, const char**)’ [-fpermissive]

To my surprise I could find no help on the matter either. This Why can't I convert 'char**' to a 'const char* const*' in C? solution did not work.

So I am left wondering: how can I pass the char ** into const char ** ?

The full function is

#include <errno.h>   // Error number definitions
#include <rrd.h>
#include <wordexp.h>

void splitToArgs(string& parametersString) //parametersString contains space separated words (parameters). 
{
    wordexp_t we;
    int er = 0;

    if ( (er=wordexp(parametersString.c_str() , &we, 0)) != 0)
    {
        cout << "error in word expansion " <<  er << endl;
    }
    else
    {
        if (we.we_wordc>0)
        {
            char * filename = we.we_wordv[1]; //filename is part of the parameters string
            rrd_clear_error();
            int ret = rrd_update_r( filename , NULL , we.we_wordc, we.we_wordv );
            if ( ret != 0 )
            {
                cout << "rrd_update error # = " << ret << " error string = " << rrd_get_error() ;
            }
        }
    }
    wordfree(&we);
}

This use of const_cast (if correct) also does not work

error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive]
const char **w = const_cast<char**>(we.we_wordv);
Community
  • 1
  • 1
nass
  • 1,453
  • 1
  • 23
  • 38

2 Answers2

1
 const_cast<const char**>(whatever)

is the correct way to const_cast in this case.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
-1

Im going to give you an example passing a char to a enum ( which is in fact a constant )

enum TIngrediente
{
    TOMATE,
    QUESO,
    NATA,
    CEBOLLA,
    POLLO,
    HUEVO,
    SALAMI,
    ANCHOA,
    BACON,
    GAMBA
};
string tolower(string s)
{
    string r = s;
    for (int i = 0; i < s.size(); ++i)
        r[i] = tolower(r[i]);
    return r;
}

TIngrediente StrToIngrediente(string s)
{
    s=tolower(s);
    int i;

     while (i < INGREDIENTES.size() and INGREDIENTES[i] != s)
        ++i;
    return (TIngrediente)i;
}

Now the only thing you need to do is make another function changing again from const char to char ( very easy) This way youll trans form the char constant to a const char, be ware youll need to add a library, cctype and local to make this work.