gpt4 book ai didi

c - 别名函数的新名称 (C)

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

我一直在尝试在 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;

最佳答案

尝试替换这些行:

queue_t (*create_queue)() = NULL; // Setting the function pointer to NULL
create_queue = &create_list; // Aliasing create_queue() for create_list()

用这一行:

queue_t (*create_queue)() = &create_list; // Aliasing create_queue() for create_list()

我的编译没有错误。

关于c - 别名函数的新名称 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27170048/

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