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 - 字符串和字符数组如何在 C 中工作?

c - 交换数组内的奇数和偶数,C

PHP从mysql中获取数组中的多个数据

java - 存储 String 数组和搜索的所有组合

C 错误 : prototype function declaration not in scope

c - 在 C 中使用指针遍历多维数组

C:递归计算矩阵行列式的程序

C++ 修改字符串时遇到问题

java - 字符串 - 用零填充前导零

c - 请求成员不是结构或 union ,而是结构