在c中查找数组中的颜色 int locate_color(常量数组[], 无符号整数列, 无符号整数行, uint8_t颜色, 无符号整数*x, 无符号整数*y) { 对于(int z=0;z
这个函数是一个从数组中查找颜色的函数。它从左到右、从上到下搜索,找到后,它将坐标存储到*x和*y中。但是当我运行代码时,它给了我一个错误。有人能告诉我哪里出错了吗?const uint8\u t数组是一维数组。我相信y您需要一个二维数组,如下所示:在c中查找数组中的颜色 int locate_color(常量数组[], 无符号整数列, 无符号整数行, uint8_t颜色, 无符号整数*x, 无符号整数*y) { 对于(int z=0;z,c,colors,coordinates,locate,C,Colors,Coordinates,Locate,这个函数是一个从数组中查找颜色的函数。它从左到右、从上到下搜索,找到后,它将坐标存储到*x和*y中。但是当我运行代码时,它给了我一个错误。有人能告诉我哪里出错了吗?const uint8\u t数组是一维数组。我相信y您需要一个二维数组,如下所示: int locate_color( const uint8_t array[], unsigned int cols, unsigned int rows, uint8_t co
int locate_color( const uint8_t array[],
unsigned int cols,
unsigned int rows,
uint8_t color,
unsigned int *x,
unsigned int *y )
{
for (int z = 0; z < rows; z++)
{
for (int c = 0; c < cols; c++)
{
if (array[z] == color)
{
*x = color;
}
if (array[c] == color)
{
*y = color;
}
return 1;
}
return 0;
}
数组[z][c];
对于(int z=0;z
您有几个问题:
- 您需要以不同的方式访问数组元素,例如:
这将偏移指针array[z][c]; for (int z = 0; z < rows; z++) { for (int c = 0; c < cols; c++) { if (array[z][c] == color) { *x = color; *y = color; return 1; // return 1 only if you find the matching color. } } }
数组
衰减到
(行索引)乘以行的长度,再加上z
(列索引),然后取消对它的引用以获得要与c
颜色进行比较的元素
- 您需要将找到颜色的行(
)和列(z
)设置为c
和*x
),而不是设置为颜色本身*y
- 在内部
循环的末尾缺少一个大括号(for
)}
- 您只需要一个
来检查您是否找到了颜色,您可以在其中设置if
和x
坐标y
if (*(array + z * cols + c) == color)
你问题的标题没有描述问题。您最好将其设置为:在循环中出现
错误
,或者类似的内容。
if (*(array + z * cols + c) == color)
{
*x = c;
*y = z;
return 1;
}