【问题标题】:Invalid to compare similar data types?比较相似的数据类型无效?
【发布时间】:2021-05-04 05:33:15
【问题描述】:

我正在开发一个应用程序项目,以找出在德州扑克游戏中获胜的概率。

我的程序有以下 3 种数据类型。

typedef enum {
  SPADES,
  HEARTS,
  DIAMONDS,
  CLUBS,
  NUM_SUITS
} suit_t;

struct card_tag {
  unsigned value;
  suit_t suit;
};
typedef struct card_tag card_t;

struct deck_tag {
      card_t ** cards;
        size_t n_cards;
};
typedef struct deck_tag deck_t;

我在编写断言函数以检查洗牌后卡片是否重复时遇到了一个问题。错误说我正在比较无效的数据类型。 错误信息的图片是:

Error Message

所以从技术上讲,我仍在比较相似的数据类型,但它们是无效的。谁能帮帮我?

【问题讨论】:

  • 请将所有代码和错误信息以文本形式直接发布在问题中。
  • 请勿发布文字图片。以后,将文本复制并粘贴到问题中。
  • 为什么struct deck_tag中的指针指向指针?

标签: c pointers relational-operators


【解决方案1】:

您不能使用== 来比较两个结构。 == 的操作数必须是算术或指针。而是比较他们的成员:

card_t a = card_from_num(checkingCard);
card_t b = *d->cards[i];
if (a.value == b.value && a.suit == b.suit) …

你可以为此编写一个函数:

_Bool CardsAreEqual(card_t a, card_t b)
{
    return a.value == b.value && a.suit == b.suit;
}
…
if (CardsAreEqual(card_from_num(checkingCard), *d->cards[i]) …

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    相关资源
    最近更新 更多