为什么我的字符串比较失败? [英] Why does my string comparison fail?

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

问题描述

比方说,我有以下的code和输出:

Let's say I have the following code and output:

for (j = 0; j <= i; j++) 
    printf("substring %d is %s\n", j, sub_str[j]);

输出:

substring 0 is max_n=20
substring 1 is max_m=20

现在我只想打印一些子。但是,如果我尝试这样做有条件:

Now I only want to print some substrings. However, if I try to do this conditionally:

for (j=0; j <=i; j++) {
   if (sub_str[j] == "max_n=20") {
      printf("substring %d is %s\n", j, sub_str[j]);
   }
}

我得到任何输出。这有什么错我的code?

I get no output at all. What's wrong with my code?

推荐答案

您不能使用 == 来比较C.字符串必须使用 STRCMP

You can't use == to compare strings in C. You must use strcmp.

for (j=0; j<=i; j++) { 
   if (strcmp(sub_str[j], "max_n=20") == 0) { 
      printf("substring %d is %s\n", j, sub_str[j]); 
   } 
} 

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

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