How to correctly convert const char * returned by a function to const char ** in C?

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);
}
+3
7

, , . , , :

const char* fileName;
config_setting_lookup_string(..., &fileName);
(...)
return fileName;

const char * . , . , ( , , , ). , fileName "getString()" , , .

0

, const char**, :

const char *s = getString();
myFunction(&s);

s , const char** , :

const char **sp = malloc(sizeof(const char *));
*sp = getString();
return sp;

+3

string , , (, , ) , . , .

?

+1

, config_setting_lookup_string() , . string, , .

. config_setting_lookup_string() , :

const char *fileName = NULL;
config_setting_lookup_string(foo, "bar", &fileName);
+1

. , , , , getString().

0

stringPtr, (string). , .

? .

: , , . :

value = &string;

value , , .

. T, :

void foo(T* value)
{
    *value = GetT();
}

T const char*:

...
*value = string;
...

- . , , ( ), . , :

  • config_setting_lookup_string do assert(value != NULL).
  • , . :

    const char * foo; config_setting_lookup_string (..., & foo);

NOT:

const char** foo;
config_setting_lookup_string(..., foo);
0

nornagon caf,

const char *fileName;
config_setting_lookup_string(foo, "bar", &fileName);

but if you can change config_setting_lookup_string, you can also do it like this:

int config_setting_lookup_string(..., const char *&value)
{
  ...
  const char *string = config_setting_get_string(member);
  value = string;
  ...
}

const char *fileName;
config_setting_lookup_string(foo, "bar", fileName);
0
source

Source: https://habr.com/ru/post/1744653/


All Articles