1

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?

2
  • if(i->code < j->code) /* if you're only comparing one character, you can just compare the character */ Commented May 8, 2011 at 22:19
  • Do you want case to be significant? You might want to use tolower() or one of the related functions before comparing values. Commented May 8, 2011 at 22:24

4 Answers 4

3

char is an integer type.

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

Sign up to request clarification or add additional context in comments.

Comments

3

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)

Comments

2

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

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

Comments

0

Try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.