Skip to main content
gamedev.net
🔒 Locked

void* pointers arrggghhh

Started by mattd Aug 31, 2000 at 4:22 AM 10 replies 900+ views
Original Post
mattd
mattd
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...

kris vanhoof
kris vanhoof
i must admit i''m more familiar with C++, but shouldn''t you put a conCommand pointer in your node struct? (just guessing)
Tornado
Tornado
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
Goblineye EntertainmentThe road to success is always under construction
DeVore
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
DeVore
DeVore
sorry for the multiple post - I kept getting invalid url errors when I pressed the reply button
-DeVore
alexmoura
alexmoura
You can use the delete option on when you edit a post

by the way, shouldn''t it be
((conCommand*)(myNode.data))->name = whatever;

I might have exagerated on the ()''s



You know, I never wanted to be a programmer...

Alexandre Moura

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.