In short, I would like to do this:
const char **stringPtr = &getString();
However, I understand that you cannot and on rvalues. So I'm stuck with this:
const char *string = getString();
const char **stringPtr = &string;
I can live with two lines. I imagine problems with this hack? I should not have been afraid to skip stringPtrfrom the function in which it is declared, right?
Edit: My apologies are not initially, including the full context. I took a summer video game project from scratch in C using OpenGL for graphics. I am reading configuration data from a text file using libconfig .
One of the convenient functions for finding a specific line from the configuration file is as follows:
int config_setting_lookup_string(const config_setting_t *setting,
const char *name, const char **value)
{
config_setting_t *member = config_setting_get_member(setting, name);
if(! member)
return(CONFIG_FALSE);
if(config_setting_type(member) != CONFIG_TYPE_STRING)
return(CONFIG_FALSE);
*value = config_setting_get_string(member);
return(CONFIG_TRUE);
}
, value, undefined, segfault. value , :
const char *dummyPtr;
const char **fileName = &dummyPtr;
config_setting_lookup_string(foo, "bar", fileName);
, , . , :
int config_setting_lookup_string(const config_setting_t *setting,
const char *name, const char **value)
{
config_setting_t *member = config_setting_get_member(setting, name);
if(! member)
return(CONFIG_FALSE);
if(config_setting_type(member) != CONFIG_TYPE_STRING)
return(CONFIG_FALSE);
const char *string = config_setting_get_string(member);
value = &string;
return(CONFIG_TRUE);
}