Thao Nguyen
Thao Nguyen

Reputation: 901

Strcmp pointer from integer without cast

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?

Upvotes: 1

Views: 8554

Answers (4)

orlp
orlp

Reputation: 117681

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)

Upvotes: 3

Daniel Lubarov
Daniel Lubarov

Reputation: 7924

Try

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

Upvotes: 0

user142162
user142162

Reputation:

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

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

Upvotes: 2

James McNellis
James McNellis

Reputation: 355069

char is an integer type.

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

Upvotes: 3

Related Questions