You need to enable JavaScript to run this app. C++比较两个数组时出现编译错误该如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

C++比较两个数组时出现编译错误该如何解决?

C语言数组比较遇到的问题

我在比较两个数组时遇到了若干问题:

int array[3] = {23, 14, 1};

if (array == {12, 43, 1}) {
    // Do something
}

我得到的报错信息如下:

error: expected primary-expression before '{' token

       if (array == {12, 43, 1}) {
                        ^

error: expected ')' before '{' token

我还尝试了如下自定义比较函数的写法:

bool compareArrays(int firstArray[], int secondArray[], int length) {
    for (int i = 0; i < length; i++) {
        if (firstArray[i] != secondArray[i]) {
            return false;
        }
    }

    return true;
}

但调用该函数时又出现了另一个报错:

if (compareArrays(array, {12, 43, 1}, 3)) {
    // Do something
}

报错详情为:

error: cannot convert '<brace-enclosed initializer list>' to 'int*' for argument '2' to 'bool compareArrays(int*, int*, int)'

该如何解决这个问题呢?提前感谢各位的帮助。


内容的提问来源于stack exchange,提问作者oscar0urselli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.25 16:24:05