简体   繁体   中英

How to find an element in an array of structs in C?

I have to write a function that finds a product with given code from the given array. If product is found, a pointer to the corresponding array element is returned.

My main problem is that the given code should first be truncated to seven characters and only after that compared with array elements.

Would greatly appreciate 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]);
}

Why don't you just use strncmp in a loop?

struct product *find_product(struct product_array *pa, const char *code) 
{ 
   for (size_t i = 0; i < pa->count; ++i)
   {
      if (strncmp(pa->arr[i].code, code, 7) == 0)
          return &pa->arr[i];
   }
   return 0;
}

temp is a pointer which is uninitialized and you are dereferencing it which will lead to undefined behavior.

temp = malloc(size); // Allocate some memory size = 8 in your case

One more mistake I see is

if (pa->arr[j].code == temp[i]) // i is already indexing `\0` 

should be

strcmp(pa->arr[j].code,temp); // returns 0 if both the strings are same

This code can completely be avoided if you can use strncmp()

As pointed out by others, you are using temp uninitialized and you are always comparing characters with '\\0' .

You don't need a temp variable:

int strncmp ( const char * str1, const char * str2, size_t num );

Compare characters of two strings

Compares up to num characters of the C string str1 to those of the C string str2.

/* Don't use magic numbers like 7 in the body of function */
#define PRODUCT_CODE_LEN 7

struct product *find_product(struct product_array *pa, const char *code) 
{   
    for (int i = 0; i < pa->count; i++) {
        if (strncmp(pa->arr[i].code, code, PRODUCT_CODE_LEN) == 0)
            return &(pa->arr[i]);
    }
    return NULL; /* Not found */
}

When you write char* temp; you are just declaring an uninitialized pointer

In your case since you say that the code is truncated to 7 you could create a buffer on the stack with place for the code

char temp[8];

Writing

temp[i] = (*code);
code++;
i++;

Can be simplified to:

temp[i++] = *code++;

In your loop

for (int j = 0; j < pa->count; j++)
    if (pa->arr[j].code == temp[i])
        return &(pa->arr[j]);

You are comparing the address of code and the character value of temp[i] which incidentally could be 8 and outside the array.

Instead what you want to do is compare what code points to and what temp contains :

for (int j = 0; j < pa->count; j++)
    if (!strncmp(pa->arr[j].code, temp, 7)
        return &(pa->arr[j]);

You should also return NULL; if nothing was found, seems you do not return anything.

Probably a good thing is also to make sure your temp[] always contains 7 characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM