Reputation: 117
I have a question regarding char pointers.
I am reading a file in C, using fgets
. This is a short overview so you can understand what I would like to do:
char configline[configmax_len + 1]; //configmax_len is the max value I need
while(fgets(configline, sizeof(configline), config){ //config is the file
char *configvalue = strtok(configline, " ");
if (configvalue[0] == "#"){
continue;
}
…
}
char * configvalue
is a pointer to the current line being read. What I would like to check is if the first character of the line is a "#".
However when I do the if statement: if (configvalue[0] == "#")
, the compiler throws an error: comparison between pointer and integer
.
How could I check if the first character of the string a pointer is pointing to is a certain value?
Upvotes: 4
Views: 12449
Reputation: 948
Strtok returns a pointer to a nul terminated string, but you're comparing this with a string constant using ==
:
if (configvalue[0] == "#")
Firstly, configvalue
is a pointer, so you could do something like:
if (*configvalue == '#')
To dereference the pointer and get the first character in the output string.
Upvotes: 1
Reputation: 49803
Use single quotes to denote a single character; double quotes denote strings, which are represented by a pointer to the first character, hence the error message.
Upvotes: 6