【问题标题】:C: comparing two strings from a structC:比较结构中的两个字符串
【发布时间】:2016-10-20 14:17:57
【问题描述】:

我几乎是 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 等于 surname 的值而不是 surname 的地址?

我还收到另一个警告:

"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)"

谁能解释为什么会出现这个警告?

【问题讨论】:

  • 变量me1Surnameme2Surname单个字符。用实际的结构成员调用strcmp
  • 感谢您的回答。这是否涉及编写以下行surnameResult = strcmp(*(me1->surname), *(me2->surname));
  • 不要使用解引用操作符。这将为您提供指针指向的值。因为指针指向单个字符,指针的取消引用将为您提供单个字符。将呼叫中的星号去掉strcmp
  • 谢谢。这似乎已经解决了!

标签: c string pointers struct strcmp


【解决方案1】:

你太努力了:

尝试显而易见的方法:

int me_compare(const MEntry *me1, const MEntry *me2) 
{
  return strcmp(me1->surname, me2->surname);
}

【讨论】:

  • 谢谢,我认为它有效。不过我想知道,那条线如何判断两个值是否相同?
  • 我的笔记函数定义如下:int strcmp(const char s[], const char t[])。我想知道为什么函数可以比较两个指针值而不是它们指向的 char 值,在这种情况下。
  • 1. int strcmp(const char s[], const char t[])int strcmp(const char *s, const char *t) 完全相同。 2、strcmp不比较指针值,而是比较st参数指向的字符串。
  • 啊,所以它们在技术上是不同的功能,那么做同样的事情?
  • 不,只有一个 strcmp函数。此函数可以声明int strcmp(const char s[], const char t[])int strcmp(const char *s, const char *t)。这是同一事物的两种不同表示法。
【解决方案2】:

比较字符串而不使用字符串库。 此方法比较字符串是否相同,如果两个字符串相等,则返回值 0。在方法里面传入struct指针就行了。

int compareStr(char *s, char *t)
{

    char t1 = *s;
    char t2 = *t;
    int x;

    while (t1 != '\0' && t2 != '\0') {
        x = (int)(t1 - t2);
        if (x ==0) {
            s++;
            t++;
            t1 = *s;
            t2 = *t;
        }
        else
        {
            break;
        }
    }


    return x;

}

【讨论】:

    【解决方案3】:

    你可以试试这个简单的例子。只需要创建两个MEntry 结构对象来测试它,并通过使用它们的地址来比较结构中的指针姓氏。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {
        char *surname;
        int house_number;
        char *postcode;
        char *full_address;
    } MEntry;
    
    int me_compare(MEntry *me1, MEntry *me2);
    
    int
    main(void) {
        MEntry me1 = {"McLeod", 27, "3432", "27 Baker Street, London"};
        MEntry me2 = {"Baggins", 19, "3242", "145 Bag End, Shire"};
    
        if (me_compare(&me1, &me2) == 0) {
            printf("Surnames are identical.\n");
        } else {
            printf("Surnames are different.\n");
        }
    
        return 0;
    }
    
    int 
    me_compare(MEntry *me1, MEntry *me2) {
        int surnameResult;
    
        surnameResult = strcmp(me1->surname, me2->surname);
    
        return surnameResult;
    }
    

    【讨论】:

    • 谢谢。棒极了。让我测试一下。
    • 正在从文件中解析数据以创建新的 MEntry
    • Meldrew, Victor 1 Happenstance Place, London N11 3SR 我们获取完整的地址行并将其解析为 MEntry 结构。然后我创建的 MEntry 将放入一个动态哈希表中。
    • 不错不错。好吧,您看起来好像找到了答案。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    相关资源
    最近更新 更多