gpt4 book ai didi

c - C中二维数组内的一维数组比较

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:28 26 4
gpt4 key购买 nike

我想在特定函数中比较两个不同大小的数组。我写了一个这样的函数:

static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])
{
static int index=0;
static bool isequal = false;

for(int i = 0 ; i<count_current; i++ )
{
for(int j = 0; j<6 ; j++ )
{
if (array_1[j] == array_2[i][j])
{
index++ ;
}
else
{
index=0;
return false;
}
}
if (index == 6)
{
index=0;
isequal = true;
i = count_current + 2;
}
else
{
index=0;
isequal = false;
}
}
return isequal;
}

定义一些变量;

static bool matching_array;
static uint8_t one_dimensional_array[6];
static uint8_t two_dimensional_array[10][6];

然后我就这样使用这个函数;

matching_array= compare_arrays( one_dimensional_array, two_dimensional_array);

但是我得到了这样的错误;

..\..\..\main.c(857): error:  #98: an array may not have elements of this type

你有什么建议吗?

最佳答案

static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])

第二个维度必须存在 - 否则编译器无法决定当有人像 array_2[y][x] 这样写时要跨越多少位置。

static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][6])

实际上,二维数组会衰减为指向其第一个元素的指针 - 类型为 uint8_t (*)[6]。并且单个 dim 数组将在此处类似地衰减为 uint8_t*。请记住,编译器不考虑传递的数组的第一个维度。数组声明必须定义除第一个之外的所有大小。

关于c - C中二维数组内的一维数组比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49022942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com