nugh
nugh

Reputation: 135

Char * Pointers vs Integers in C

I am trying to compare two characters in C But for some reason it is comparing an Integer, and a character. How can I convert a character pointer to an integer, so I can use it with the isspace() method?

Here is part of my code:

char *token;
token = strtok(line, " ");
int i = 0;
while (token != NULL)
{
    if(*token != ':' || isspace(token)){
        printf("%i:--%s--\n", i, token);
    }
    token = strtok(NULL, " ");
    i++;
}

Upvotes: 2

Views: 53

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83527

You already do it correctly with *token != ':'. Just use the same syntax: isspace(*token).

Upvotes: 1

chux
chux

Reputation: 153447

int isspace(int ch) expects a value in the unsigned char range or EOF. It does not take a pointer as used by char *token; .... isspace(token)

Instead reference the char pointed to by token. As char may be as signed char or unsigned char, insure unsigned char values are used when passed to is...() functions.

// isspace(token)
isspace((unsigned char) *token)

if(*token != ':' || ...){ is strange as that passes all non-':' characters, which includes all white-space ones. This portion of OP's code is unclear.

Upvotes: 3

Related Questions