我的程序输入有一个数组:
//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/