Cka91405
Cka91405

Reputation: 155

warning: dereferencing ‘void *’ pointer

I am doing a homework and I was wondering if I anyone could help me figure out what the error " warning: dereferencing ‘void *’ pointer " means in reference to my code. Thank you very much.

Error Warning:

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

Upvotes: 3

Views: 13410

Answers (1)

keety
keety

Reputation: 17441

list is of void* type so it should be

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

you cannot dereference a void* pointer you need to cast it back to the node type if you want to access the node structure. Read through void pointer to get a better understanding

Upvotes: 3

Related Questions