为什么我的 strcmp() 失败了? [英] Why is my strcmp() failing?

查看:70
本文介绍了为什么我的 strcmp() 失败了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C 新手,正在学习字符串标记化.我试图通过以下方式比较两个字符串.但是我正在做的字符串比较失败了.

I am a C newbie and learning string tokenizing. I am trying to compare two strings in the following way. But the string comparison I am doing is failing.

你能告诉我我在这里遗漏了什么吗?

Can you please let me know what I am missing here?

我找不到另一个类似的问题,可能是由于我在 C 方面的经验不足.如果有的话,请您将我重定向到它吗?

I couldn't find another similar question, may be due to my inexperience in C. If one exists, can you please redirect me to it?

char* input = "comparer here";

char* args[5];

int counter = 0;
char *tok = strtok(input, " ");
while (tok != NULL) {
   args[counter] = tok;
   counter ++;
   if (counter == 5)
     break;
   tok = strtok(NULL, " ");
}

char* comp_str = "comparer";    
if (strcmp(args[0], comp_str) == 1) {
        // do some stuff
}

推荐答案

失败,因为 strcmp(及其兄弟)如果相等则返回零值,如果第一个小于则返回负值比第二个,如果第一个大于第二个,则为正值.

It fails because strcmp (and its siblings) returns a zero value if they are equal, a negative value if the first is less than the second, and a positive value if the first is greater than the second.

未指定负值或正值.在大多数实现中,它是第一个不同字符的差异.但这并不能保证.

The negative or positive value is not specified. In most implementations it is the difference of the first different characters. But that is not guaranteed.

将结果与 1 进行比较不太可能成功.

Comparing the result to 1 is very unlikely to succeed.

这篇关于为什么我的 strcmp() 失败了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆