如何将常量字符**路径分配为字符串



我想在运行时获取OS X的工作可执行文件目录。 我在下面找到了这段代码,但我不知道如何将const path用作字符串。 它抱怨:

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

我想将const char **路径分配为字符串。 下面是代码:

#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 : %sn", s);
  return 0;
}

更改:

std::string s = path.c_str();
printf("path is : %sn", s);

自:

std::string s(path[0]); // create C++ std::string and initialise
                        // from C string `path[0]`
std::cout << "path is : " << s << std::endl;

最新更新