0

So I have this Linked List print function that is giving me the error:

error: invalid use of void expression

Here's the line that causes this error:

printf("[%d] -> ", *(link->pointer)); /*note: i tried to cast it to (int) but still     same error. */

Here's my struct for the link; very straight forward:

typedef struct DLLNode {
    void * pointer;
    struct DLLNode *next;
    struct DLLNode *previous;
} DLLNode;

I'm using my prepend function as so:

...
int j = 2;
prepend(&j, list);
...

so that it uses the pointer to variable two as the value the struct stores in new DLLNode as the pointer.

Could someone tell me why this is happening and what it means? Thanks for your time.

ChrisMcJava
  • 2,145
  • 5
  • 25
  • 33

1 Answers1

2

I suppose you are trying to print the value that link->pointer points to.
printf("[%d] -> ", var) expects to see either an integer in its argument list or something that can be converted to it (for example, a char).

link->pointer is of type void * and after dereferencing, it looks like a void type variable. Thus, compiler can't convert *(link->pointer) to int type. To tell it that you actually keep an integer value behind that void * pointer you need to explicitly convert link->pointer to a int * type, as Kaz pointed out in the comments and only then dereference it. I hope the following code demonstrates this more clearly:

DLLNode *list;
// initialization and other stuff

void *p = list->pointer;
int *p_i;
p_i = (int*)p; // explicit cast to int*

// print the integer value pointed to by the list->pointer
printf("[%d] -> ", *p_i);