I want to check if a line begins with the charatcer "#". My current code gives me a segmenation fault when I run it. How do I get the first character from a char*?
int checkLine(char* line){
char l [250];
strcpy(l,line);
char first_char = l[0];
//Check for comment
if (first_char == "#"){
return 0;
}
return 1;
}
CodePudding user response:
you just got to change the If statement, instead of using "#" you should use '#', as you're representing a char, not a String. Btw, there is no need to copy your first char into a variable, you can use if (l[0]=='#')
CodePudding user response:
int checkLine(char* line)
{
return ('#'==*line);
}
should be enough. Usually it happens that the empty spaces also need to be skipped before to check for '#'.
So you could do this way as well:
int checkLine(char* line)
{
while (' '==*line) line ;
return ('#'==*line);
}