
dereferencing pointer to a structure containing a pointer to int
Quote:
> How do I dereference this thing?
> typedef struct tagNode{
> struct tagNode * pPrev, * pNext;
> void * data;
> }Node;
> int main(void){
> Node pNode = (Node*)malloc(sizeof(Node)):
> int num = 5;
> pNode->data = #/*Also, where should the explicit cast go? Pnode->(int*)data?*/
pNode->data = (void *) #
Althought the above line will work, be careful! You don't normally take
pointers of automatic variables and assign them to data structures
because if you leave the function which declares the variable, the pointer
will no longer be valid.
Quote:
> printf("%d\n", pNode->data);/* I don't know how do deref. I want to print num*/
printf("%d\n", *(int *)pNode->data);
You can't dereference a plain void* due to the fact that C has no idea what
you're pointing at. With the cast shown above you are explicitly saying,
"this pointer is to an int, then dereference it".
Quote:
> return(0)
> }
> I am printing out the address of num, but I can't figure out how to deref the
> darned thing. I have been guessing like crazy, so I am asking for help!
> Any help would be appreciated.
> Thanks in advance,
> Ed
--
Download complete C source to my Dynace Object Oriented
Extension to C and Windows Development System from:
http://www.edge.net/algorithms
Algorithms Corporation - 615-791-1636 - USA