-1

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
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Typecase your pointer to the object it is pointing to. In this case, as `node` – brokenfoot Mar 17 '14 at 22:44
  • 1
    @brokenfoot the problem seems that it could probably either be a `node` or a `root`, at least that is my guess. – Shafik Yaghmour Mar 17 '14 at 22:45
  • safest to have a type and be the first element, or a struct that holds either a root or a node – Grady Player Mar 17 '14 at 22:49
  • possible duplicate of [access element of struct passed into a void\* pointer](http://stackoverflow.com/questions/12995287/access-element-of-struct-passed-into-a-void-pointer) – UpAndAdam Mar 17 '14 at 22:59
  • possible duplicate of http://stackoverflow.com/questions/14946344/casting-a-void-pointer-to-a-struct?rq=1 as well – UpAndAdam Mar 17 '14 at 23:00

1 Answers1

1

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.

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • I think you mean "cast" or "typecast" instead of typecase. Note this this will blow up if parent is Null. – UpAndAdam Mar 17 '14 at 22:48
  • but the parent can be a pointer to root type struct too, parent wont be null because i fill them in recursive functions edit: typo – user3430912 Mar 17 '14 at 22:48
  • corrected it, but this is what OP wants to do. Yes, he'll have to handle that case too.. – brokenfoot Mar 17 '14 at 22:49
  • Yes, that is true, I just highlighted why he's getting the error and how to avoid it. Atleast the case for which he's getting the error. – brokenfoot Mar 17 '14 at 22:50
  • @user3430912 You say it won't be null, but given that you are asking this question, please strongly consider to develop the good programming habit now and make sure that you check pointers before dereferencing them. Maybe you use assert, maybe you check it yourself, maybe you don't. If you had any idea how many bugs have resulted from that very statement you'd be laughing. – UpAndAdam Mar 17 '14 at 22:56
  • 1
    @user3430912: Yes, it could be either type of pointer, you need to have some way of knowing it and then de-reference your pointer accordingly. I updated my answer. – brokenfoot Mar 17 '14 at 23:00
  • ok then i'll use 2 different functions for children of root and children of node nodes. Thanks – user3430912 Mar 18 '14 at 00:21