Question

I have an assignment about a labyrinth solving algorithm and I used a path tree to solve it, these are my structs:

typedef struct node* nodePtr;
typedef struct root{
    int coordX;
    int coordY;
    nodePtr child[4];
} root;

typedef struct node{
    int coordX;
    int coordY;
    char val;
    nodePtr child[3];
    void* parent;
} node;

The parent pointer can be either a pointer to root or a pointer to node to not to have a loop in tree. I checked this thing on assigning nodes:

void assignNode(nodePtr *nodeAddr, int x, int y, char **maze, void *parent){
...some codes...
if(y != parent->coordY && x != parent->coordX)

This is where I get annoying error of

dereferencing 'void *' pointer
error: request for member 'coordY' in something not struct or union
error: request for member 'coordX' in something not struct or union
Was it helpful?

Solution

You can't de-reference a void* like this. It needs to know the type of pointer (in your case either node* or root*). So, you need to typecast your pointer (either node* or root*). In this particular case, as node. ie instead of

if(y != parent->coordY && x != parent->coordX)

write

if(y != ((node*)parent)->coordY && x != ((node*)parent)->coordX)

But since it your code, as you mentioned it could be either kind of the pointer (node* or root*), you need to have some way to identify it and then do the typecasting appropriately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top