关于typedef的问题

C语言 码拜 8年前 (2016-04-04) 1274次浏览
代码如下:
struct Node;
typedef struct Node *PtrToNode;
问一下哪位可以解释下第二句的意思。
解决方案

6

struct Node;  声明一个结构体
typedef struct Node *PtrToNode;  定义一个结构体指针

6

typedef
typedef type-declaration synonym;
The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition.
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.
Example
// Example of the typedef keyword
typedef unsigned long ulong;
ulong ul;     // Equivalent to “unsigned long ul;”
typedef struct mystructtag
{
int   i;
float f;
char  c;
} mystruct;
mystruct ms;   // Equivalent to “struct mystructtag ms;”
typedef int (*funcptr)();  // funcptr is synonym for “pointer
//    to function returning int”
funcptr table[10];   // Equivalent to “int (*table[10])();”

16

typedef struct Node *PtrToNode;
意思是:
给struct Node *取个别名叫PtrToNode(注意*是跟着前面的)
struct Node *a; 等同于PtrToNode a;

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于typedef的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)