Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在结构数组中搜索_C_Arrays_Struct - Fatal编程技术网

C 在结构数组中搜索

C 在结构数组中搜索,c,arrays,struct,C,Arrays,Struct,我不知道这是否是最好的解决方案,但这是我经过长时间搜索后发现的全部: 我想在一个数组中搜索mystring,如果找到它,我就可以看到这个国家。这就是我到目前为止所做的,但是使用结构数组有点复杂,所以我恳请您的帮助 char *mystring = "butter"; typedef struct user_data { char* company; char* country; }user_data; user_data comp[]={ { .company = "Compa

我不知道这是否是最好的解决方案,但这是我经过长时间搜索后发现的全部:

我想在一个数组中搜索mystring,如果找到它,我就可以看到这个国家。这就是我到目前为止所做的,但是使用结构数组有点复杂,所以我恳请您的帮助

char *mystring = "butter";

typedef struct user_data {
 char* company;
 char* country;
}user_data;


user_data comp[]={
    { .company = "Company selling Eggs", .country  = "United Kingdom" },
    { .company = "Company selling Butter", .country  = "United States" },
    .....................   //other structures (around 200)
};

我怎样才能使用strcmp呢?

你必须使用
strstr()
而不是
strcmp()

inti;

对于(i=0;i您必须使用
strstr()
而不是
strcmp()

inti;

对于(i=0;iA loop?使用
sizeof(comp)/sizeof(comp[0])
获取数组中的条目数。@ShaMora是问题所在的循环还是strcmp(item.company,mystring)?我想他想在公司字符串中查找butter的出现情况。A loop?使用
sizeof(comp)/sizeof(comp[0])
获取数组中的条目数。@ShaMora是问题所在的循环还是strcmp(item.company,mystring)?我想他想在公司字符串中查找butter的出现情况。
int i;
for (i=0; i<sizeof(comp)/sizeof(comp[0]); i++) {
    if (strstr(comp[i].company, mystring))
         printf("Country is: %s\n", comp[i].country)
}