gpt4 book ai didi

C - 这两个结构声明有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:39 26 4
gpt4 key购买 nike

typedef struct hash_table_data
{
int key;
int data;
struct hash_table_data* next;
struct hash_table_data* prev;
}hash_table_data;

typedef struct hash_table
{
int num_entries;
struct hash_table **entries;
}hash_table;

对比

struct hash_table_data_
{
int key,data;
struct hash_table_data_ *next,*prev;
};
typedef struct hash_table_data_ hash_table_data;

struct hash_table_
{
int num_entries;
struct hash_table_data_ **entries;
};
typedef struct hash_table_ hash_table;

最佳答案

在第二个示例中,您有一个额外的名称:struct hash_table_data_struct hash_table_

在第一个示例中,您使用将 struct hash_table_data 重命名为更短的 hash_table_data 的 typedef 有效地隐藏了名称 struct hash_table_data

可以这样想:

typedef (something that happens to be struct X) (to) X

对比

define a struct X
typedef struct X (to) X

现在在后一个例子中,你实际上并没有完全按照前一个例子做。在后一个例子中,你做

define a struct X_
typedef struct X_ (to) X

这里的关键是您有一个可以直接使用的“额外”结构 X_。

关于C - 这两个结构声明有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com