Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回字符数组函数错误_C++ - Fatal编程技术网

C++ 返回字符数组函数错误

C++ 返回字符数组函数错误,c++,C++,可能重复: 这份申报表有什么问题?我尝试使用以下函数返回当前路径,但它似乎不正确: 请不要:我需要一个字符返回而不是字符串 char* getINIfile(void) { char buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); string::size_type pos = string( buffer ).find_last_of( "\\/" ); string path =

可能重复:

这份申报表有什么问题?我尝试使用以下函数返回当前路径,但它似乎不正确:

请不要:我需要一个字符返回而不是字符串

char* getINIfile(void)
{
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    string path = string( buffer ).substr( 0, pos) + "\\setup.ini";

    char *ini_local= (char*)path.c_str();

    printf(ini_local); // so far output OK!

    return ini_local;
}

main
{
    printf(getINIfile()); // output Not OK! 

    char mybuffer[200];
    GetPrivateProfileStringA( "files","DLL","0",  mybuffer,200, getINIfile());
    printf(mybuffer);

}

当函数退出时,返回的地址超出了范围,因此不再有效:
std::string path
是函数
getINIFile
的本地地址,因此在函数退出后它无效,从
path.c_str()
获得的地址也是无效的

在这种情况下,您可以从函数返回
std::string
。如果以后确实需要C字符串,可以使用
C_str()
,然后:

std::string getINIfile(void)
{
    //...

    return path;
}


int main()
{
    string path = getINIFile();

    // do something with path.c_str():
    const char *cPath = path.c_str();
}
考虑到您的代码,我想不出任何理由您必须有一个
char*
返回,但如果是这样,您需要在堆上分配一个缓冲区:

char *getINIfile(void)
{
    char *buffer[MAX_PATH];
    GetModuleFileName(NULL, buffer, MAX_PATH);
    string::size_type pos = string(buffer).find_last_of( "\\/" );
    string path = string(buffer).substr( 0, pos) + "\\setup.ini";

    char *ini_local = new[path.size()];
    strncpy(ini_local, path.c_str(), path.size());

    printf(ini_local); // so far output OK!

    return ini_local;
}
但是这是标准C字符串和标准字符串的一种非常糟糕的混合:只需使用
string
来操作路径并在其他地方传递
char*

仅使用标准C,将中的
find_last_替换为-注意缺少错误处理:

char *getINIfile(void)
{
    char *buffer = new[MAX_PATH];
    char *pos = NULL;
    char *ini_local = NULL;

    GetModuleFileName(NULL, buffer, MAX_PATH);
    pos = strrchr(buffer, "\\/");
    // check for and handle pos == NULL

    buffer[pos] = '\0';

    strncat(buffer, "\\setup.ini", MAX_PATH - strlen(buffer));

    printf(buffer);

    return buffer;
}

函数返回一个指向局部变量的指针,该变量超出范围,留下一个悬空的指针。为什么不直接按值返回
std::string

std::string getINIfile() {
   ....
   return path;
}
然后,您可以在调用者端使用字符串的底层
char*

const std::string s = getINIfile();
const char* c = s.c_str();

路径在函数末尾超出范围,您将在该超出范围对象中返回一个内部指针。请尝试返回std::字符串

std::string getINIfile(void)
{
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    string path = string( buffer ).substr( 0, pos) + "\\setup.ini";

    char *ini_local= (char*)path.c_str();

    printf(ini_local); // so far output OK!

    return path;
}

到底为什么。。。你是用这样一种糟糕的方式组合C++和C吗?要么使用C,使用适当的方法,要么使用C++,远离字符数组。我们有
std::string
是有原因的。我删除了C标记,因为这不是C(不管你怎么努力)。这里也有一个根本的误解,;不能从函数返回数组。指针不是数组。另外,我想指出的是,您使用的是
printf
unformatted。请看一个很好的安全漏洞:我需要一个字符数组返回。这样仍然会返回我最初拥有的字符串。@Power Mosfet您可以从返回的字符串中获取
const char*
。看看我的答案。