简体   繁体   English

如何将const char ** path分配为字符串?

[英]How to assign const char ** path as a string?

I want to get the working executable directory at runtime for OS X. I found this code below, but I don't know how to use the const path as a string. 我想在OS X的运行时获取工作的可执行目录。我在下面找到了这段代码,但是我不知道如何将const path用作字符串。 It's complaining that: 它抱怨:

testingtesting.cxx:7:30: warning: format specifies type 'char *' but the
  argument has type 'const char **' [-Wformat]
printf("path is : %s\n", path);

I want to assign the const char ** path as a string. 我想将const char **路径分配为字符串。 Here is the code below: 这是下面的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>

using namespace std;

int main (int argc, const char *argv[], const char *env[], const char *path[]) {
  // path[0] now contains the path to the executable
  // NOT PORTABLE! OS X ONLY!

    std::string s = path.c_str();

    printf("path is : %s\n", s);
  return 0;
}

Change: 更改:

std::string s = path.c_str();

printf("path is : %s\n", s);

to: 至:

std::string s(path[0]); // create C++ std::string and initialise
                        // from C string `path[0]`

std::cout << "path is : " << s << std::endl;

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

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