1
votes

I'm trying to compare two character and see which one is lexicographic longer and sorting by it problem is I'm not sure how to compare single character I tried doing it with strcmp like

struct example
{
 char code;
}
if (strcmp(i->code, j->code) < 0)
    return 1;

warning: passing argument 1 of âstrcmpâ makes pointer from integer without a cast

warning: passing argument 2 of âstrcmpâ makes pointer from integer without a cast

I know that strcmp is for strings, should I just malloc and make the char code into a string instead, or is there another way to compare single characters?

4
if(i->code < j->code) /* if you're only comparing one character, you can just compare the character */ - forsvarir
Do you want case to be significant? You might want to use tolower() or one of the related functions before comparing values. - John Carter

4 Answers

3
votes

char is an integer type.

You compare char objects using the relational and equality operators (<, ==, etc.).

3
votes

strcmp() takes a null-terminated string, so in C a char *. You are passing it a single character, a char. This char gets automatically promoted to an int in the expression (this always happens with char's in expressions in C) and then this int is attempted to change to a char*. Thank god this fails.

Use this instead:

if (i->code < j->code)
2
votes

Since you're comparing chars and not null terminated strings, do the following:

if (i->code < j->code)
0
votes

Try

if (i->code < j->code)
    ...