【问题标题】:Incompatible types when assigning to type 'struct PartyInfo' from type Int从 Int 类型分配给类型“struct PartyInfo”时的类型不兼容
【发布时间】:2015-12-06 22:32:43
【问题描述】:
int main() {

struct PartyInfo maxVoteParty;
maxVoteParty = maxVoteParty = GetMaxVote(partiesInfo, noPartyLists);

printf("\n");

return 0;
}

struct PartyInfo GetMaxVote(struct PartyInfo array[], int partyListNumber)
{
    int maxVote = 0;
    int i;
    struct PartyInfo maxVoteParty;

    for (i = 0; i < partyListNumber; i++)
    {
        if (maxVote == 0 || maxVote < array[i].votes)
        {
            maxVote = array[i].votes;
            maxVoteParty = array[i];
        }
    }

    return maxVoteParty;
}

你好,

我在以下行收到上述错误:

maxVoteParty = maxVoteParty = GetMaxVote(partiesInfo, noPartyLists);

我对 C 语言非常陌生,但我使用过 Java、C#、Python 和许多其他语言,但我遇到了很多 C 语言的新问题。

【问题讨论】:

  • 把原型struct PartyInfo GetMaxVote(struct PartyInfo array[], int partyListNumber);放在int main() {之前
  • 这一行:maxVoteParty = maxVoteParty = GetMaxVote(partiesInfo, noPartyLists); 可能不正确,因为它已将maxVoteParty 分配给自己。
  • 将一个结构分配给另一个相同类型的结构时,请使用:memcpy() 或单独分配每个字段。
  • “通常”最好将指针传递给结构,而不是传递整个结构。
  • 传递给 GetMaxVote() 的几个参数未在发布的代码中定义,因此我们无法调试此编译问题。 struct PartyInfo的定义是什么?

标签: c types


【解决方案1】:

在第一次实际调用函数之前,您缺少 GetMaxVote 函数的函数原型。

struct PartyInfo GetMaxVote(struct PartyInfo array[], int partyListNumber);

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2016-03-25
    • 2021-01-17
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2016-01-23
    • 2016-02-24
    相关资源
    最近更新 更多