Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mattd

void* pointers arrggghhh

This topic is 6479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
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 generic

node* pNext;
} myNode;

myNode->data = (void*)myCmnd->name;
// Now myNode->data will return myCmnd->name.





The road to success is always under construction

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
sorry for the multiple post - I kept getting invalid url errors when I pressed the reply button
-DeVore

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!