简体   繁体   中英

First character of pointer

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?

try using

if (configvalue[0] == '#'){

this should compile nicely

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM