C 编程。比较数组的内容。

标签 c arrays string strcmp

我的程序输入有一个数组:

//1.

int i, numberOfOccurances;

   for(i = 0; i < numOfOccurrances; i++) {
      printf("%d",PrintOccurrances[i]);
   }

作为示例输出:

121

现在我想比较这个数组,以便我可以打印附加语句,例如:

//2.

if (PrintOccurrances == 121) {
    printf("This means blah");
} else if (PrintOccurrances == 232) {
    printf("This means something else");
}

//我应该设置什么类型的变量以及如何在点 1 处设置它? //在第 2 点我应该使用什么类型的字符串语句。

感谢您的帮助。

最佳答案

创建一个比较函数并在调用站点使用复合文字:

#include <stdbool.h>

bool int_arr_cmp_n(int const * a, int const * b, size_t len)
{
    while (len--)
        if (*a++ != *b++)
            return false;
    return true;
}

用法:

if (int_arr_cmp_n(PrintOccurrances, (int[]){1,2,1}, 3)) { /* ... */ }

关于C 编程。比较数组的内容。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16630375/

相关文章:

c - 为什么所有的数组元素都被初始化为 0 即使我们几乎不给前几个元素赋值

c - popen:拦截用户的输入

arrays - SWIFT - 如何获取数组的类型?

c - 字符串数组初始化

C++:将整数转换为罗马数字的困惑

swift - 从 NSMutableAttributedString 中删除项目符号点

javascript - 在 vue 中作为 prop 传递的对象以字符串形式到达

c - 搜索字符串中的公共(public)字符并返回指向公共(public)字符的指针(不使用strpbrk)

c - 如何正确链接 libssh?

php - zabbix一段时间内动态创建主机组