Function for getting configuration causes segfault
I'm sort of a self-taught beginner programmer here. One thing I always tried to avoid was pointers. After this last project, I figured I had to start learning them, as well as improving what other skills I might have.
This program is supposed to read a line from a configuration file and pass it back to whatever requests it, be it a variable or another function. It always causes a segfault.
It's probably something stupidly simple that I'm overlooking here, but I'm really new to these things. Any help would be greatly appreciated!
Code:
int main()
{
char *blah;
blah = getconf(false,"test");
printf("%s\n",blah);
return(0);
}
char *getconf(bool writelog,char get[])
{
char line[256];
FILE *config;
int a = 0;
char setting[25];
char *value;
config = fopen("test.conf","r");
while(fgets(line,sizeof(line),config) != NULL) {
a++;
if(line[0] == '#') continue; //If the line is a comment, skip it
if(sscanf(line,"%s = %s",setting,value) != 2) {
if(writelog == true) {
printf("%i: syntax error!\n",a);
}
continue;
}
puts("return if");
if(strcmp(setting,get)) return(value); //GDB backtrace always shows the segfault around this area.
puts("val not found");
}
if(writelog == true) {
printf("%s: value not found\n",get);
}
return("no value");
}