C:比较结构中的两个字符串

标签 c string pointers struct strcmp

我几乎是 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/

相关文章:

c - 指向字符串数组的指针

c - 从文件中读取字符串

c++ - 加密和解密部分 ASCII 表

c - 在终止我的 TCP/IP 连接后使用 POSIX "write"函数会使我的应用程序崩溃 - 为什么?

c - 使用 C 动态分配结构

c++ - 查找失败时 boost string_algo 返回值

python - 如何在 Python 中将一组字符串拆分为子字符串,使更短的子字符串更有可能?

c++ - 指针在 C++ 中占用的动态分配内存量

c++ - 使用哪种(自动)指针?

c - 使用 pcap 解析跟踪文件?