I’m trying to compare a string stored in a structure. I’m using scanf to input a string and I need to search for the string in the structure. Here is the code I have:
int printing_airports(int all_routes_size,int all_airports_size){
int i,position;
char airport;
printf("Enter airport code: ");
scanf("%s", airport);
for(i=0;i<all_airports_size;i++){
if(strcmp(airport,all_airports_divid[i].code) == 0){
position = i;
}
}
printf("%s",all_airports_divid[position].code);
}
This is how I declared my structures
struct all_routes{
int id;
char departure_code[4];
char arrival_code[4];
};
struct all_routes all_routes_divid[500];
This is the error I’m getting when I tried to compile
y:~/enee150/project2: compile
functions.c: In function 'printing_airports':
functions.c:370:15: error: invalid type argument of unary '*' (have 'int')
functions.c:373:5: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]
/usr/include/iso/string_iso.h:64:12: note: expected 'const char *' but argument is of type 'char'
What am I doing wrong? Please help me.
You declare
airportas acharbut use it as a string. Declare it
char airport[100];or so.scanfrequires a pointer to the variable the result shall be stored in as argument. When declared aschar,airportis passed as aninttoscanf, that is why the first message mentionsint. The argument is dereferenced (unary*) to find the location where to store the scan result.strcmprequires a pointer (to a 0-terminated string) as argument. If passed an integral type likechar, without explicit cast, the compiler warns about that (because 99.99+% of the time it’s wrong to do that).