我几乎是 C 语言的新手,想知道如何比较来自两个独立结构成员变量的字符串。也许提供我的代码会使我的要求更加清晰。
我有以下结构:
typedef struct mentry {
char *surname;
int house_number;
char *postcode;
char *full_address;
} MEntry;
我想比较两个单独的 MEntry 变量。我想检查两个条目的姓氏是否相同。因此,我编写了以下方法:
int me_compare(MEntry *me1, MEntry *me2)
{
int surnameResult;
char me1Surname = *(me1->surname);
char me2Surname = *(me2->surname);
surnameResult = strcmp(me1Surname, me2Surname);
return surnameResult;
}
当我编译我的程序时,我收到以下消息:
mentry.c:30:6: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
surnameResult = strcmp(me1Surname, me2Surname);
我认为这条线是错误的吗:
char me1Surname = *(me1->surname);
将 me1Surname 设置为姓氏的值而不是姓氏的地址?
我还收到另一个警告:
"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)"
谁能解释为什么会出现这个警告?
最佳答案
你太努力了:
尝试显而易见的方法:
int me_compare(const MEntry *me1, const MEntry *me2)
{
return strcmp(me1->surname, me2->surname);
}
关于C:比较结构中的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40157028/