2012-03-28 103 views
3

我正在做功課,我想知道是否有人可以幫助我找出錯誤「警告:解除引用」void *'指針「是指引用我的代碼。非常感謝你。警告:解除引用'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 
+2

我敢打賭,答案隱藏在'ABSTRACT_TYPE'宏後面 - 那是什麼?我猜這裏面有一個'void *'。 – 2012-03-28 22:05:56

+1

錯誤的行號與您發佈的代碼不匹配 – 2012-03-28 22:06:59

+0

我只是修復了我在發佈後意識到它後遺憾大衛 – Cka91405 2012-03-28 22:09:12

回答

3

名單void*類型,因此它應該是

`DATA(*plst) = data;` 
`NEXT(*plst) = NULL;` 

你不能解引用你需要的,如果你要訪問的節點結構,將它轉換回節點類型爲void *指針。通過void pointer閱讀以獲得更好的理解

+0

現在我得到一個錯誤,說circular_list.c:42:warning:'free_node'定義但沒有使用它是:static void free_node(list * p_lst) (開始她的第42行:)「{」 free(* p_lst ); * p_lst = NULL; } – Cka91405 2012-03-28 23:24:51

+1

再次在免費你是解除引用一個void *類型,該函數應該是無效free_node(list_rep * p_lst) – keety 2012-03-28 23:29:23

+0

我改變了它,我仍然得到錯誤錯誤是關於「{」im不知道什麼這意味着 – Cka91405 2012-03-29 00:08:35