来自整数的 Strcmp 指针,无需强制转换
我正在尝试比较两个字符,看看哪个字符的字典顺序更长,并按它排序问题是我不确定如何比较单个字符我尝试使用 strcmp 进行操作,如
struct example
{
char code;
}
if (strcmp(i->code, j->code) < 0)
return 1;
警告:传递“strcmp”的参数 1 使指针来自整数而不需要强制转换
警告:传递“strcmp”的参数 2 使整数指针无需强制转换
我知道 strcmp 用于字符串,我应该直接 malloc 并将 char 代码转换为字符串,还是有其他方法进行比较单个字符?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
char
是整数类型。您可以使用关系运算符和相等运算符(
<
、==
等)来比较char
对象。char
is an integer type.You compare
char
objects using the relational and equality operators (<
,==
, etc.).strcmp()
接受一个以 null 结尾的字符串,因此在 C 中是char *
。您向其传递一个字符,即char
。该 char 在表达式中自动提升为int
(这种情况总是发生在 C 表达式中的char
),然后这个int
是尝试更改为char*
。感谢上帝,这失败了。使用这个代替:
strcmp()
takes a null-terminated string, so in C achar *
. You are passing it a single character, achar
. This char gets automatically promoted to anint
in the expression (this always happens withchar
's in expressions in C) and then thisint
is attempted to change to achar*
. Thank god this fails.Use this instead:
由于您要比较的是字符而不是空终止字符串,因此请执行以下操作:
Since you're comparing chars and not null terminated strings, do the following:
尝试
Try