标准库中的strstr()函数用来检索子串在字符串中首次出现的位置,但是使用这个函数时字符串中不能包含 0x00。因此,写了一个函数在一个大的数组中匹配一个小的数组,返回子数组在数组中首次出现的位置指针。
uint8_t *Array_Find_Subarray(uint8_t *Array, uint16_t Array_Effective_Count, uint8_t *Subarray, uint16_t Subarray_Effective_Count)
{
for (uint16_t i = 0; i < (Array_Effective_Count - Subarray_Effective_Count); i++)
{
for (uint16_t n = i,j = 0; j < Subarray_Effective_Count; n++,j++)
{
if (Array[n] == Subarray[j])
{
if ((Subarray_Effective_Count - 1) == j)
return &Array[i];
}
else
{
break;
}
}
}
return NULL;
}
1
int8_t *Array_Find_Subarray(uint8_t *Array, uint16_t Array_Effective_Count, uint8_t *Subarray, uint16_t Subarray_Effective_Count)
{
for (uint16_t i = 0; i < (Array_Effective_Count - Subarray_Effective_Count); i++)
{
for (uint16_t j = 0; j < Subarray_Effective_Count; j++)
{
if (Array[i + j] == Subarray[j])
{
if ((Subarray_Effective_Count - 1) == j)
return &Array[i];
}
else
{
break;
}
}
}
return NULL;
}