搜索数组中的元素
Searching elements in an array
我想使用 uint16_t 类型的变量在 uint8_t 类型的数组 days[] 中搜索元素的索引,该元素的下一个元素为 0x2 和 0x3 并打印元素“0x2”的索引值。我已经分配了 'uint16_t search = 0x23' 但我不确定这是否有效
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t days[5] = {0x1,0x2,0x3,0x4,0x5};
uint8_t *ptr = days;
uint16_t search = 0x23;
for(uint16_t i=0;i<5;i++){
if (*(ptr+i) == search){
printf("%d", i);
break;
}else{
printf("not found at %d\n", i);
}
}
return 0;
}
为了能够将uint8数组成对搜索为uint16,需要在比较前将数组指针从uint8转换为uint16。以下是代码的样子:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t days[5] = {0x1,0x2,0x3,0x4,0x5};
uint8_t *ptr = days;
uint16_t search = 0x0302;
for(uint16_t i=0;i<4;i++){
if (*((uint16_t *) (ptr+i) ) == search){
printf("%d", i);
break;
}else{
printf("not found at %d\n", i);
}
}
return 0;
}
请注意,我正在使用 (ptr+i)
将 (uint16_t *)
递增为 uint8_t
的指针投射,以便以 uint8_t
[ 的步骤遍历数组=24=]
另请注意,我已将循环次数减少到 4 次而不是 5 次,因为通过将数组的最后一个元素转换为 uint16_t
,您将超出数组的边界。
最后,您搜索的值应该是 0x0302
而不是 0x23
。首先,因为 0x03
和 0x02
是将存储在 uint8
中的值,其次,由于 CPU 的 endianness
,它们将转换为 uint16_t
时在内存中的顺序相反
Note: When using cast you should be certain how the cast will be interpreted, meaning not to overrun the boundaries of the variables, otherwise you might cause segmentation faults, or even silent problems which will lead to an undefined behavior of your program.
也许你想要这个:
uint8_t days[5] = { 0x1,0x2,0x3,0x4,0x5 };
uint16_t search = 0x23;
for (uint16_t i = 0; i < 4; i++) { // 4 not 5 because of days[i + 1] below
uint16_t value = (days[i] << 4) | days[i + 1];
if (value == search)
{
printf("%d", i);
break;
}
else
{
printf("not found at %d\n", i);
}
}
我想使用 uint16_t 类型的变量在 uint8_t 类型的数组 days[] 中搜索元素的索引,该元素的下一个元素为 0x2 和 0x3 并打印元素“0x2”的索引值。我已经分配了 'uint16_t search = 0x23' 但我不确定这是否有效
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t days[5] = {0x1,0x2,0x3,0x4,0x5};
uint8_t *ptr = days;
uint16_t search = 0x23;
for(uint16_t i=0;i<5;i++){
if (*(ptr+i) == search){
printf("%d", i);
break;
}else{
printf("not found at %d\n", i);
}
}
return 0;
}
为了能够将uint8数组成对搜索为uint16,需要在比较前将数组指针从uint8转换为uint16。以下是代码的样子:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t days[5] = {0x1,0x2,0x3,0x4,0x5};
uint8_t *ptr = days;
uint16_t search = 0x0302;
for(uint16_t i=0;i<4;i++){
if (*((uint16_t *) (ptr+i) ) == search){
printf("%d", i);
break;
}else{
printf("not found at %d\n", i);
}
}
return 0;
}
请注意,我正在使用 (ptr+i)
将 (uint16_t *)
递增为 uint8_t
的指针投射,以便以 uint8_t
[ 的步骤遍历数组=24=]
另请注意,我已将循环次数减少到 4 次而不是 5 次,因为通过将数组的最后一个元素转换为 uint16_t
,您将超出数组的边界。
最后,您搜索的值应该是 0x0302
而不是 0x23
。首先,因为 0x03
和 0x02
是将存储在 uint8
中的值,其次,由于 CPU 的 endianness
,它们将转换为 uint16_t
Note: When using cast you should be certain how the cast will be interpreted, meaning not to overrun the boundaries of the variables, otherwise you might cause segmentation faults, or even silent problems which will lead to an undefined behavior of your program.
也许你想要这个:
uint8_t days[5] = { 0x1,0x2,0x3,0x4,0x5 };
uint16_t search = 0x23;
for (uint16_t i = 0; i < 4; i++) { // 4 not 5 because of days[i + 1] below
uint16_t value = (days[i] << 4) | days[i + 1];
if (value == search)
{
printf("%d", i);
break;
}
else
{
printf("not found at %d\n", i);
}
}