I need to write a function that finds a product with a given code from a given array. If the product is found, a pointer to the corresponding array element is returned.
My main problem is that this code must first be truncated to seven characters and only after that compared to the elements of the array.
Many thanks for your help.
struct product *find_product(struct product_array *pa, const char *code)
{
char *temp;
int i = 0;
while (*code) {
temp[i] = (*code);
code++;
i++;
if (i == 7)
break;
}
temp[i] = '\0';
for (int j = 0; j < pa->count; j++)
if (pa->arr[j].code == temp[i])
return &(pa->arr[j]);
}
source
share