简体   繁体   English

如何将字符串转换为char *数组

[英]How to convert string into char * array

I have changed my code, now while the compilation these errors occur: 我已经更改了代码,现在在编译时会出现以下错误:

`check.cpp: In function ‘int main()’:`

check.cpp:14:55: error: invalid conversion from 'const char**' to 'char* const*' [-fpermissive]

/usr/include/getopt.h:152:12: error: initializing argument 2 of 'int getopt(int, char* const*, const char*)' [-fpermissive]

int main() {

string text="-f  input.gmn -output.jpg";
int argc=text.length();
cout<<"argc: "<<argc<<endl;
char const * argv = text.c_str();
cout<<"argv: "<<argv<<endl;
int c = getopt (argc, &argv, "f:s:o:pw:h:z:t:d:a:b:?");
return 0;
}

You can use text.c_str() to convert a std::string into a const char* . 您可以使用text.c_str()std::string转换为const char* See here . 这里

To elaborate on my answer, there are many ways to create the array you need, but this is already described here , here , here and here . 要详细说明我的答案,有很多方法可以创建所需的数组,但这已在此处此处此处此处进行了描述。 A simple solution to your problem that does not involve new / malloc or intensive uses of the STL and istringstream / back_inserter / copy what not and performs really fast could look like this: 一个简单的解决方案,它不涉及new / malloc或STL和istringstream / back_inserter / copy大量使用,而istringstream并没有很快执行,可能看起来像这样:

/* variables. */
std::vector< char* > argv;
int i, argc, state;
char c;

/* convert string to char string with automatic garbage collection. */
std::vector< char > tokens(text.begin(), text.end());
tokens.push_back(0);

/* tokenize string with space. */
for (state=0, argc=0, i=0; (c=tokens[i]); i++) {
    if (state) {
        if (c == ' ') {
            tokens[i]=0;    
            state=0;        
        }           
    } else {
        if (c != ' ') {
            argv.push_back(&tokens[i]);
            argc++;         
            state=1;        
        }           
    }       
}   

/* print argv. */
std::cout << "argc: " << argc << std::endl;
for (i=0; i < argc; i++) {
    std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
}   

/* call getopt. */
c = getopt(argc, &argv[0], "f:s:o:pw:h:z:t:d:a:b:?");

This is just an example, but one advantage of this kind of code is that you can use other characters as delimiter, not just space, and that you need not care about releasing the allocated memory since std::vector does this for you on function exit. 这只是一个例子,但是这种代码的一个优点是您可以使用其他字符作为分隔符,而不仅仅是空格,并且您不必担心释放分配的内存,因为std::vector在功能上为您做到了出口。

First bug with your code is the comparison: 您的代码的第一个错误是比较:

for (int i=0; i<=stringLength; i++) {
   arv[i]=text[i]; 
}

Use i< stringLength instead of i<=stringLength . 使用i< stringLength而不是i<=stringLength

The second bug is that arv is not null-terminated. 第二个错误是arv不是以null结尾的。

After fixing both bugs, your code should look like this: 修复两个错误之后,您的代码应如下所示:

for (int i=0; i < stringLength; i++) {
   arv[i]=text[i]; 
}
arv[stringLength] = '\0';

By the way, the correct function signature of getopt is this: 顺便说一句, getopt正确函数签名是这样的:

int getopt(int argc, char * const argv[], const char *optstring);

which takes second and third argument as const . 将第二个和第三个参数作为const That means, you can do this: 也就是说,您可以执行以下操作:

 char const * s = text.c_str();
 int c = getopt (argc, &s, "f:s:o:pw:h:z:t:d:a:b:?");

No need of any conversion, using manual loop. 无需任何转换,使用手动循环。

In short, you have an array argv which contains 100 pointers to strings, of which only the first is set. 简而言之,您有一个argv数组,其中包含100个指向字符串的指针,其中仅设置了第一个。 argv[1] hasn't been set to anything, so is pointing somewhere random. argv [1]尚未设置为任何值,因此它指向的是随机位置。 And in this case, illegal. 在这种情况下,是非法的。

Moreoever, what getoption expects is going to be more like this: 此外,getoption期望的结果将更像这样:

argv[0] = "progname";
argv[1] = "-f";
argv[2] = "input.gmn"
argv[3] = "-output.jpg"
argv[4] = 0

Note the =0 at the end to stop getoption chargins through random bits of memory 请注意,最后的= 0会停止通过内存的随机位获取getoption字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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