简体   繁体   English

如何在C中找到结构数组中的元素?

[英]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. 如果找到product,则返回指向相应数组元素的指针。

My main problem is that the given code should first be truncated to seven characters and only after that compared with array elements. 我的主要问题是给定的代码应首先截断为7个字符,然后才与数组元素进行比较。

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? 你为什么不在循环中使用strncmp?

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是一个未初始化的指针,你正在取消引用它,这将导致未定义的行为。

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() 如果你可以使用strncmp()完全可以避免这个代码

As pointed out by others, you are using temp uninitialized and you are always comparing characters with '\\0' . 正如其他人所指出的,你正在使用temp未初始化,并且你总是将字符与'\\0'进行比较。

You don't need a temp variable: 您不需要temp变量:

int strncmp ( const char * str1, const char * str2, size_t num ); 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. 将C字符串str1的最多num个字符与C字符串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; 当你写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 在您的情况下,因为您说代码被截断为7,您可以在堆栈上创建一个缓冲区,其中包含代码的位置

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. 您正在比较code的地址和temp[i]的字符值,它偶然可能是8并且在数组之外。

Instead what you want to do is compare what code points to and what temp contains : 相反,你想要做的是比较什么代码和temp 包含什么:

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; 你还应该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. 可能一件好事也是为了确保你的temp []总是包含7个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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