• Advertisement

Archived

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

void* pointers arrggghhh

This topic is 6381 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
i must admit i''m more familiar with C++, but shouldn''t you put a conCommand pointer in your node struct? (just guessing)

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
An example of how I always do this sort of thing in good old C.
(I even compiled and ran it in VC++ 6.0 as a Win32 console application just to make sure. ) Enjoy.


#include
#include

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

typedef struct node
{
void *data;
struct node *next;
} myNode;

int main(int argc, char* argv[])
{
char str1[10] = "A STRING",
str2[10];

myCmnd cmd;

myNode head1,
*head2 = (myNode *) malloc(sizeof(myNode));

cmd.name = str1;
cmd.foo = 1;

head1.data = (myNode *) &cmd;
head2 -> data = (myNode *) &cmd;

strcpy(str2, ((myCmnd *)(head1.data)) -> name);
printf("%d %s\n", ((myCmnd *) (head1.data)) -> foo, str2);

strcpy(str2, ((myCmnd *)(head2 -> data)) -> name);
printf("%d %s\n", ((myCmnd *) (head2 -> data)) -> foo, str2);

return 0;
}


Mike Roberts
aka milo
mlbobs@telocity.com

Share this post


Link to post
Share on other sites

  • Advertisement