I am writing a program that reads a value from an .ini file and then passes the value to a function that accepts PCSTR (i.e. const char *). Function getaddrinfo().
So, I want to write PCSTR ReadFromIni(). To return a constant string, I plan to allocate memory with malloc()and translate the memory into a constant string. I can get the exact number of characters that were read from the .ini file.
Is this technology good? I really don't know what else to do.
The following example works fine in Visual Studio 2013 and prints "hello" as desired.
const char * m()
{
char * c = (char *)malloc(6 * sizeof(char));
c = "hello";
return (const char *)c;
}
int main(int argc, char * argv[])
{
const char * d = m();
std::cout << d;
}