值之间的差异以及代码如何读取 CS50 中的值

Difference between values and how the code reads the values in CS50

这是我的主要代码:

// Update vote totals given a new vote
bool vote(string name, int candidatecount, candidate candidates1[MAX])
{
    // TODO
    for (int a = 0; a < candidatecount; a++)
    {
        if (candidatecount == a)
        {
            printf("a\n");
            return false;
        }
        if (name == candidates1[a].names)
        {
            printf("b\n");
            candidates[a].votes = candidates[a].votes + 1;
        }
        printf("%s, %s\n", candidates1[a].names, name);
    }
    printf("1\n");
    return(name);
}

这是我的命令行:

    ./plurality R D

输出(包括我的回复)是这样的:

    Number of voters: 3
    Vote: R
    R, R
    D, R
    1
    Vote: D
    R, D
    D, D
    1
    Vote: R
    R, R
    D, R
    1
    vote candidate 0 go around 0
    b 0 e (null)
    vote candidate 0 go around 1
    b 0 e (null)
    (null) won with 0 votes. 

它看起来像这样:

    if (name == candidates1[a].names)

应该可以,但不行,因为我在它之后的 printf 不起作用。 有谁知道为什么?

回顾 Week 3 video,从 40 分钟左右开始。教授说

It turns out that in C, you can't use equals equals to compare two strings.

他当时就介绍了strcmp,下节课会详细解释为什么。简单的解释是字符串是指针,== 将比较操作数的 地址 ,而不是 .