【发布时间】:2014-11-27 11:49:12
【问题描述】:
我一直在尝试在 C 中实现队列结构,使用 C 中链表的另一种实现。
队列需要的一些函数已经在linked_list.h中定义了,我想给它们取一个新名字;例如,为 create_list() 别名 create_queue()。
由于我对 C 编程相当陌生,因此我在网上查找了一种方法来执行此操作,然后遇到了函数指针的话题。这似乎是做我想做的事情的正确方法,但是当我尝试它时:
#include "linked_list.h"
typedef list_t queue_t; // list_t is the list type defined in linked_list.h,
// as well as the functions such as create_list()
queue_t (*create_queue)() = NULL; // Setting the function pointer to NULL
create_queue = &create_list; // Aliasing create_queue() for create_list()
当我尝试编译时,我收到错误和警告:
- 错误:使用不同类型重新定义“create_queue”:“int”与“queue_t (*)()”
- 警告:缺少类型说明符,默认为 'int'
我的代码中缺少什么?我不想要我的问题的完整解决方案,只是重定向到正确的方式。
这是 create_list():
list_t create_list(){
/* Creates and returns a NULL list, with index and value zeroed */
list_t list;
malloc(sizeof(list_t));
list.global_index = 0; /* Global index to keep how many nodes in the list, since no loops are allowed */
list.total_value = 0; /* Total value to keep total value of nodes in the list, since no loops are allowed */
list.head = NULL;
list.last = NULL;
return list;
}
以及结构定义:
struct int_node {
int value;
struct int_node * previous; /* Needed to go to previous nodes with no loops */
struct int_node * next;
};
struct int_list {
int global_index;
int total_value;
struct int_node * head;
struct int_node * last;
};
/* Typedefs */
typedef struct int_node node_t;
typedef struct int_list list_t;
【问题讨论】:
-
请给出原型和你的错误所在
-
我添加了 create_list() 原型。错误和警告都在同一行:create_queue = &create_list;
-
你能把
list_t的结构也展示一下吗? -
你能显示编译器警告你的那一行吗?
-
@Ilay 和报错是同一行:create_queue = &create_list;
标签: c linked-list queue alias typedef