void* pointers arrggghhh

Started by
10 comments, last by mattd 23 years, 8 months ago
arrrgghhhh i hate void pointers! ok, lets say i have
    

struct node {
  void* data;         // designed to be generic

  node* pNext;
} myNode;

struct conCommand {
  char* name;
  int foo;
} myCmnd;

    
why doesn''t myNode->myCmnd->name work?

"The attitudes and personality of the programmer is reflected in the written program." So that's why my programs have waaaay too many bugs, aren't user-friendly, and swear when they create errors...

Advertisement
i must admit i''m more familiar with C++, but shouldn''t you put a conCommand pointer in your node struct? (just guessing)
You trying to access MyCmnd->name from myNode->data ?
In that case you should:

    struct conCommand {  char* name;  int foo;} myCmnd;struct node {  void* data;         // designed to be genericnode* pNext;} myNode;myNode->data = (void*)myCmnd->name;// Now myNode->data will return myCmnd->name.    




The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
I the problem is that you have forgotten to cast your pointers to the correct type.

say you initialise like this:

myNode.data = &myCmnd

first of all, to access the name field of the myCmnd struct, you would write something like:

(conCommand*(myNode.data))->name = whatever;

Notice that myNode is not a pointer so you don''t use ''->'', you use ''.''. Second, you need to cast the void pointer to data to be a pointer to a conCommand. This is because until you tell the compiler what that pointer is pointing to, it doesn''t know how to look at the memory.

Hope it helps and is right - any comments...

-DeVore
I the problem is that you have forgotten to cast your pointers to the correct type.

say you initialise like this:

myNode.data = &myCmnd

first of all, to access the name field of the myCmnd struct, you would write something like:

(conCommand*(myNode.data))->name = whatever;

Notice that myNode is not a pointer so you don''t use ''->'', you use ''.''. Second, you need to cast the void pointer to data to be a pointer to a conCommand. This is because until you tell the compiler what that pointer is pointing to, it doesn''t know how to look at the memory.

Hope it helps and is right - any comments...

-DeVore
I the problem is that you have forgotten to cast your pointers to the correct type.

say you initialise like this:

myNode.data = &myCmnd

first of all, to access the name field of the myCmnd struct, you would write something like:

(conCommand*(myNode.data))->name = whatever;

Notice that myNode is not a pointer so you don''t use ''->'', you use ''.''. Second, you need to cast the void pointer to data to be a pointer to a conCommand. This is because until you tell the compiler what that pointer is pointing to, it doesn''t know how to look at the memory.

Hope it helps and is right - any comments...

-DeVore
I the problem is that you have forgotten to cast your pointers to the correct type.

say you initialise like this:

myNode.data = &myCmnd

first of all, to access the name field of the myCmnd struct, you would write something like:

(conCommand*(myNode.data))->name = whatever;

Notice that myNode is not a pointer so you don''t use ''->'', you use ''.''. Second, you need to cast the void pointer to data to be a pointer to a conCommand. This is because until you tell the compiler what that pointer is pointing to, it doesn''t know how to look at the memory.

Hope it helps and is right - any comments...

-DeVore
I the problem is that you have forgotten to cast your pointers to the correct type.

say you initialise like this:

myNode.data = &myCmnd

first of all, to access the name field of the myCmnd struct, you would write something like:

(conCommand*(myNode.data))->name = whatever;

Notice that myNode is not a pointer so you don''t use ''->'', you use ''.''. Second, you need to cast the void pointer to data to be a pointer to a conCommand. This is because until you tell the compiler what that pointer is pointing to, it doesn''t know how to look at the memory.

Hope it helps and is right - any comments...

-DeVore
I the problem is that you have forgotten to cast your pointers to the correct type.

say you initialise like this:

myNode.data = &myCmnd

first of all, to access the name field of the myCmnd struct, you would write something like:

(conCommand*(myNode.data))->name = whatever;

Notice that myNode is not a pointer so you don''t use ''->'', you use ''.''. Second, you need to cast the void pointer to data to be a pointer to a conCommand. This is because until you tell the compiler what that pointer is pointing to, it doesn''t know how to look at the memory.

Hope it helps and is right - any comments...

-DeVore
sorry for the multiple post - I kept getting invalid url errors when I pressed the reply button
-DeVore

This topic is closed to new replies.

Advertisement