I try to setup a c programm using the libconfig. There is example1.c:
int main()
{
const char **channel;
config_t config;
config_init(&config);
config_read_file(&config, "example.cfg");
if( config_lookup_string(&config,"value.channel",&channel) == CONFIG_FALSE)
{
printf("Failed to read fields\n");
return 1;
}
printf("argumente = %s\n", (char *)channel);
return 0;
}
and the example.cfg file
value = { channel = "hello"; }
if I compile it
gcc example1.c -lconfig
it says:
example1.c:39:3: Warning: delivery of arguments 3 from »config_lookup_string« of a incompatible pointer
/usr/include/libconfig.h:244:26: Detailed: »const char **« expected, but argument has typ »const char ***«
The funny thing is it works... the output is: argumente = hello
How can I get rid of this warning ?
If I change the decleration to const char *channel
and the output printf("argumente = %s\n", channel);
, I receive a segfault at start and a warning at compiling like ... Detailed: »const char **« expected, but argument has typ »const char *«
You just need to get rid of one *
in your declaration of channel
. If you do that, you can also remove the cast in your printf
call.
The reason it's "working" now is that cast is hiding a second warning about your printf
format. Your program is behaving just like the extra *
was removed already - just in a confusing way.