我最近开始研究结构和指针,但有些东西我还没有完全理解a的设计struct.我理解structie typedef struct Alias及其内容的声明,但我不明白Get_noAllyp并*no_getOf在声明的最后.这些是什么?我也找不到一个好的来源.
typedef struct Alias {
char *s_a_name;
char **s_aliases;
short *s_dumr;
int s_get_sum;
}Get_noAllyp, *no_getOf; /*Here, I don't understand this one.
Where did these two variables come from?
And one of them is a pointer.*/
Run Code Online (Sandbox Code Playgroud)
Mar*_*gal 14
它typedef为同一个东西定义了多个s,即多个"名称",而第二个是指向它的指针.
第一个Get_noAllyp是为struct提供的名称,同时no_getOf表示指向它的指针.
即,写作no_getOf与Get_noAllyp *在函数签名或变量声明中编写完全相同.
Sou*_*osh 10
在这里,有两个typedef以简洁的方式装箱.以上typedef可以分解为
typedef struct Alias {
char *s_a_name;
char **s_aliases;
short *s_dumr;
int s_get_sum;
}Get_noAllyp;
typedef struct Alias * no_getOf;
Run Code Online (Sandbox Code Playgroud)
所以,
Get_noAllyp 代表 struct Aliasno_getOf 代表 struct Alias *代码:
struct Alias {
char *s_a_name;
char **s_aliases;
short *s_dumr;
int s_get_sum;
}
Run Code Online (Sandbox Code Playgroud)
定义一个具有名称Alias且为a 的新数据类型struct.这种C语言的原始设计在这里有点笨拙,因为它要求结构类型名称在使用时始终以struct关键字为前缀.
这意味着代码:
struct Alias {
char *s_a_name;
char **s_aliases;
short *s_dumr;
int s_get_sum;
} Get_noAllyp, *no_getOf;
Run Code Online (Sandbox Code Playgroud)
声明Get_noAllyp类型struct Alias的变量no_getOf和类型的变量pointer to struct Alias.
通过将typedef关键字放在前面,标识符Get_noAllyp和no_getOf变为类型(而不是变量).
Get_noAllyp与(与指向结构Alias的指针)相同struct Alias并且no_getOf相同struct Alias *().
现在你可以写:
struct Alias x;
struct Alias *y;
Run Code Online (Sandbox Code Playgroud)
要么
Get_noAllyp x;
no_getOf y;
Run Code Online (Sandbox Code Playgroud)
声明x为类型的变量struct Alias和y作为类型的变量pointer to a struct Alias.