簡體   English   中英

搜索一系列結構

[英]search through an array of structures

我不知道這是否是最好的解決方案,但這是經過長時間搜索后發現的所有內容:

我想在數組中搜索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()

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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM