[英]passing a char ** to a function(const char **) in C++
I am trying to use the function rrd_update_r
of the round robin database. 我正在尝试使用循环数据库的函数
rrd_update_r
。
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
. 该函数接受众所周知的
argc, argv
作为第3和第4个参数。
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. 即使我为此项目使用C ++(和g ++),rrd也是用C编写的,因此我可以使用GNY / Linux中提供的函数
wordexp(char *, wordexp_t*)
将string
的参数拆分为argv
数组。
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. 问题是wordexp_t返回char **类型的成员(如argv),这显然与rrd_update_r函数调用不兼容。
/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?
这为什么我不能在C语言中将'char **'转换为'const char * const *'? solution did not work.
解决方案不起作用。
So I am left wondering: how can I pass the char ** into const char ** ? 所以我想知道:如何将char **传递给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 const_cast
这种用法(如果正确)也不起作用
error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive]
const char **w = const_cast<char**>(we.we_wordv);
const_cast<const char**>(whatever)
在这种情况下是const_cast的正确方法。
Im going to give you an example passing a char to a enum ( which is in fact a constant ) 我将给您一个将char传递给枚举的示例(实际上是一个常量)
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. 现在,您唯一需要做的就是让另一个函数再次从const char更改为char(非常简单),这样您就可以将char常量转换为const char,要注意,您需要添加一个库,cctype和local来进行创建这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.