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 character开发者_JS百科s?
char
is an integer type.
You compare char
objects using the relational and equality operators (<
, ==
, etc.).
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)
Since you're comparing chars and not null terminated strings, do the following:
if (i->code < j->code)
Try
if (i->code < j->code)
...
精彩评论