【问题标题】:Implementation of an abstract data type in C using typedef使用 typedef 在 C 中实现抽象数据类型
【发布时间】:2014-04-01 03:35:56
【问题描述】:

所以我在我的一本书中查看了 C 代码的 sn-p,它与使用“typedef”运算符实现抽象数据类型有关:

struct account {
  char *username;
  char *password;
};

struct account;

typedef struct account *Account;

有人可以向我解释这部分的目的是什么吗:

struct account;

我不确定它在这里有什么用处。 谢谢!

【问题讨论】:

    标签: c struct typedef abstract-data-type


    【解决方案1】:
    struct account;
    

    查看您的代码,这并不是真正必要的。但它可以用作前向声明,当您还没有完整的定义可用,并且您正在使用account 类型的指针并在其他地方定义它时。

    【讨论】:

      【解决方案2】:

      这只是一个前向声明。这转到公共标头:

      struct account;
      typedef struct account *Account;
      

      所以Account 类型在外面是可用的,但结构成员是隐藏的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 1970-01-01
        相关资源
        最近更新 更多