如何理解typedef Node * List

mcmay 2014-04-15 11:32:06
各位达人,近日看C Primer Plus中的一个单链表的头文件,其中有一个typedef看不大明白。具体情况如下:

/* list.h -- header file for a simple list type */

#ifndef LIST_H_

#define LIST_H_

#include <stdbool.h> /* C99 feature */



/* program-specific declarations */



#define TSIZE 45 /* size of array to hold title */

struct film

{

char title[TSIZE];

int rating;

};



/* general type definitions */



typedef struct film Item;



typedef struct node

{

Item item;

struct node * next;

} Node;



typedef Node * List; //这里把一个结构体 Node 定义成一个指针 * List,不是很明白,请具体讲解一下,谢谢!



/* function prototypes */



/* operation: initialize a list */

/* preconditions: plist points to a list */

/* postconditions: the list is initialized to empty */

void InitializeList(List * plist);



/* operation: determine if list is empty */

/* plist points to an initialized list */

/* postconditions: function returns True if list is empty */

/* and returns False otherwise */

bool ListIsEmpty(const List *plist);



/* operation: determine if list is full */

/* plist points to an initialized list */

/* postconditions: function returns True if list is full */

/* and returns False otherwise */

bool ListIsFull(const List *plist);



/* operation: determine number of items in list */

/* plist points to an initialized list */

/* postconditions: function returns number of items in list */

unsigned int ListItemCount(const List *plist);



/* operation: add item to end of list */

/* preconditions: item is an item to be added to list */

/* plist points to an initialized list */

/* postconditions: if possible, function adds item to end */

/* of list and returns True; otherwise the */

/* function returns False */

bool AddItem(Item item, List * plist);



/* operation: apply a function to each item in list */

/* plist points to an initialized list */

/* pfun points to a function that takes an */

/* Item argument and has no return value */

/* postcondition: the function pointed to by pfun is */

/* executed once for each item in the list */

void Traverse (const List *plist, void (* pfun)(Item item) );



/* operation: free allocated memory, if any */

/* plist points to an initialized list */

/* postconditions: any memory allocated for the list is freed */

/* and the list is set to empty */

void EmptyTheList(List * plist);



#endif

...全文
407 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiandingzhe 2014-04-16
  • 打赏
  • 举报
回复
引用 6 楼 mcmay 的回复:
谢谢楼上各位的解析。这就是说根据 typedef Node * List 这个定义, List实际上就是个 Node结构体的指针了是吗?但是我在后面的代码中又看到了这样的变量声明和函数参数传递。 List movies; InitializeList(&movies); 既然根据前面的typedef定义List本来就是一个指针了,也就是说变量movies本身就是个Node结构体指针。那么,为何把它当做函数参数传递时还要用&movies的形式呢?谢谢!
这种东西通常这么设计,glib里面用来代替throw catch机制:

return_type some_operation_that_may_cause_error(......, error_type** err);

// 调用时
error_type* err = 0;
some_operation_that_may_cause_error(..., &err);
if (err)
{
    fprintf(stderr, "error: %s\n", err->message);
    exit(1);
}
jiandingzhe 2014-04-16
  • 打赏
  • 举报
回复
引用 6 楼 mcmay 的回复:
谢谢楼上各位的解析。这就是说根据 typedef Node * List 这个定义, List实际上就是个 Node结构体的指针了是吗?但是我在后面的代码中又看到了这样的变量声明和函数参数传递。 List movies; InitializeList(&movies); 既然根据前面的typedef定义List本来就是一个指针了,也就是说变量movies本身就是个Node结构体指针。那么,为何把它当做函数参数传递时还要用&movies的形式呢?谢谢!
在初始化函数里传递指针变量的地址,可以设计成这样:如果失败,指针赋0。 当然,这里我看不到设计成这样的必要性,因为返回值也可以。
li4c 2014-04-16
  • 打赏
  • 举报
回复
typedef Node * List; //List 定义为指向结构体的指针
mcmay 2014-04-16
  • 打赏
  • 举报
回复
谢谢楼上各位的解析。这就是说根据 typedef Node * List 这个定义, List实际上就是个 Node结构体的指针了是吗?但是我在后面的代码中又看到了这样的变量声明和函数参数传递。 List movies; InitializeList(&movies); 既然根据前面的typedef定义List本来就是一个指针了,也就是说变量movies本身就是个Node结构体指针。那么,为何把它当做函数参数传递时还要用&movies的形式呢?谢谢!
h_destiny 2014-04-15
  • 打赏
  • 举报
回复
别名,便于修改,移植性好!
如此美丽的你 2014-04-15
  • 打赏
  • 举报
回复
类型重定义。有什么不好理解的?
xiaolomg 2014-04-15
  • 打赏
  • 举报
回复
相当于别名
max_min_ 2014-04-15
  • 打赏
  • 举报
回复
就是 当你定义一个List时, 其实它实际是Node*类型的! 图个方便
dbzhang800 2014-04-15
  • 打赏
  • 举报
回复
这是定义Node* 为一个新的类型 List

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧