#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define LEN_ID 3
#define LEN_P 30
#define LEN_CIDADE 50
#define AT 40
typedef struct aeroporto
{
char id[LEN_ID + 1];
char pais[LEN_P + 1];
char cidade[LEN_CIDADE + 1];
} Aeroporto;
int findavailablespot(Aeroporto l[AT])
{
int i = found = 0;
for (;i<AT;i++) {
if (l[i] = {"aaa","bbb","ccc"}) //Error in this line
break;
if (found)
return i;
else
return -1;
}
}
因此,我正在创建结构aeroporto
,然后是一个由aeroportos组成的向量,我想检查{"aaa"、"bbb"、"ccc"}是否出现在向量内部。帮助
很抱歉格式化,这个是新的
您必须使用strcmp()
来比较字符串。对于一个结构的所有成员来说,没有捷径可走,你必须单独测试每个成员,并与&&
组合。
您还忘记了在脱离循环之前设置found
。
int i = 0, found = 0;
for (;i<AT;i++) {
if (strcmp(l[i].id, "aaa") == 0 && strcmp(l[i].pais, "bbb") == 0 && strcmp(l[i].cidade, "ccc")) {
found = 1;
break;
}
}