我正在做作业,我想知道我是否有人可以帮我弄清楚错误“警告:取消引用'void *'指针”意味着参考我的代码。非常感谢你。
错误警告:
circular_list.c: In function ‘allocate_node’:
circular_list.c:36(line: DATA(lst) = data;): warning: dereferencing ‘void *’ pointer
circular_list.c:36(line: DATA(lst) = data;): error: request for member ‘datapointer’ in something not a structure or union
circular_list.c:37(line: NEXT(lst) = NULL;): warning: dereferencing ‘void *’ pointer
circular_list.c:37(line: NEXT(lst) = NULL;): error: request for member ‘next’ in something not a structure or union
circular_list.c
#include <stdio.h>
#include <stdlib.h>
#include "globals.h"
#include "circular_list.h"
typedef struct node *list_rep;
typedef struct node {
generic_ptr datapointer;
list_rep next;
} node;
#define DATA(lst) ((lst)->datapointer)
#define NEXT(lst) ((lst)->next)
#define TOLIST(list_rep) ((list) (list_rep))
#define TOREP(lst) ((list_rep)(lst))
#define TOLISTPTR(list_rep_ptr) ((list *) (list_rep_ptr))
#define TOREPPTR(lst_ptr) ((list_rep *)(lst_ptr))
static status allocate_node (list_rep *p_lst, generic_ptr data)
{
list lst = (list) malloc(sizeof(node));
if (lst == NULL)
return ERROR;
*p_lst = lst;
DATA(lst) = data;
NEXT(lst) = NULL;
return OK;
}
static void free_node (list *p_lst)
{
free(*p_lst);
*p_lst = NULL;
extern status init_circ_list (list *p_lst)
{
*p_lst = NULL;
return OK;
}
extern bool empty_circ_list (list lst)
{
return (lst == NULL) ? TRUE : FALSE;
}
status circ_insert (list *p_lst, generic_ptr data)
{
list_rep lst, *p_lst_rep = TOREPPTR(p_lst);
if (allocate_node(&lst, data) == ERROR)
return ERROR;
if (empty_circ_list(*p_lst)) {
NEXT(lst) = lst;
*p_lst_rep = lst;
} else {
NEXT(lst) = NEXT(*p_lst_rep);
NEXT(*p_lst_rep) = lst;
}
return OK;
}
circular_list.h
#include "globals.h"
#ifndef _CIRCULAR_LIST_H
#define _CIRCULAR_LIST_H
ABSTRACT_TYPE(list);
extern status init_circ_list (list *p_lst);
extern bool empty_circ_list (list lst);
extern status circ_insert (list *p_lst, generic_ptr data);
extern status circ_append (list *p_lst, generic_ptr data);
extern status circ_delete (list *p_lst, generic_ptr *p_data);
extern status circ_delete_node(list *p_lst, list node);
extern generic_ptr circ_head(list lst);
extern list circ_tail(list lst);
#endif
globals.h
#ifndef _GLOBALS_H
#define _GLOBALS_H
typedef enum {OK, ERROR} status;
typedef enum {FALSE = 0, TRUE = 1} bool;
typedef void *generic_ptr;
typedef unsigned char byte;
#define ABSTRACT_TYPE(t)
typedef void *(t)
#endif
答案 0 :(得分:3)
列表属于void*
类型,因此应为
`DATA(*plst) = data;`
`NEXT(*plst) = NULL;`
如果要访问节点结构,则无法取消引用需要将其强制转换为节点类型的void *指针。仔细阅读void pointer以更好地理解