Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:43:28+00:00 2026-06-09T03:43:28+00:00

Possible Duplicate: Is it a good idea to return “ const char * ”

  • 0

Possible Duplicate:
Is it a good idea to return “ const char * ” from a function?
how to return char array in c++?

What is wrong with this return? I’m trying to return the current path using the following function but it doesn’t seems to be correct:

Please Not: I need an char return not string.

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);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:43:30+00:00Added an answer on June 9, 2026 at 3:43 am

    You’re returning an address that goes out of scope when the function exits, and so it’s no longer valid: std::string path is local to the function getINIFile and so it’s invalid after the function exits, as is the address that you get from path.c_str().

    In this case you can just return the std::string from your function. If you really need a C string later, you can use c_str() then:

    std::string getINIfile(void)
    {
        //...
    
        return path;
    }
    
    
    int main()
    {
        string path = getINIFile();
    
        // do something with path.c_str():
        const char *cPath = path.c_str();
    }
    

    Given your code I can’t think of any reason that you must have a char* return, but if so you’ll need to allocate a buffer on the heap:

    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;
    }
    

    But this is a really awful mix of standard C strings and std::string: just using string to manipulate the path and passing around char* everywhere else.

    Using only standard C, replacing find_last_of with strrchr – note the lack of error handling:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Typedef pointers a good idea? I've seen this oddity in many APIs
Possible Duplicate: Is it a good idea to wrap an #include in a namespace
Possible Duplicate: Is this a good way to generate a string of random characters?
Possible Duplicate: Is it a good idea to learn JavaScript before learning jQuery? I
Possible Duplicate: What is the purpose of the expression “new String(…)” in Java? I've
Possible Duplicate: What is a good way to get a list of files from
Possible Duplicate: Are static inner classes a good idea or poor design? Java supports
Possible Duplicate: I need a good way to get data from a thread to
Possible Duplicate: PHP Arrays: A good way to check if an array is associative
Possible Duplicate: Is a good idea have a BaseController and make all controllers extend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.