I am almost brand new to C and was wondering how to compare strings from two separate struct member-variables. Maybe providing my code will bring clarity to what I am asking.
I have the following structure:
typedef struct mentry {
char *surname;
int house_number;
char *postcode;
char *full_address;
} MEntry;
I want to compare two seperate MEntry variables. I want to check if the surname of both entries is the same. So, I've written the following method:
int me_compare(MEntry *me1, MEntry *me2)
{
int surnameResult;
char me1Surname = *(me1->surname);
char me2Surname = *(me2->surname);
surnameResult = strcmp(me1Surname, me2Surname);
return surnameResult;
}
When I compile my program I get the following messages:
mentry.c:30:6: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
surnameResult = strcmp(me1Surname, me2Surname);
Am I wrong in thinking that the line:
char me1Surname = *(me1->surname);
sets me1Surname equal to the value of surname and not the address of surname?
I also get another warning saying:
"In file included from mentry.c:2:0:
/usr/include/string.h:140:12:note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)"
Can someone explain why this warning appears?
You are trying too hard:
Try the obvious way:
int me_compare(const MEntry *me1, const MEntry *me2)
{
return strcmp(me1->surname, me2->surname);
}