dereferencing pointer to a structure containing a pointer to int 
Author Message
 dereferencing pointer to a structure containing a pointer to int

How do I dereference this thing?

typedef struct tagNode{
        struct tagNode * pPrev, * pNext;
        void * data;

Quote:
}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?*/

printf("%d\n", pNode->data);/* I don't know how do deref. I want to print num*/

return(0)

Quote:
}

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


Sat, 23 Jan 1999 03:00:00 GMT  
 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?*/
>>printf("%d\n", pNode->data);/* I don't know how do deref. I want to print num*/

>return(0)
>}

printf("%d\n", *(int *)(pNode->data));

This will print out 5.  (Assuming you declare Node *pNode and not Node
pNode like up there).

Marcel



Sat, 23 Jan 1999 03:00:00 GMT  
 dereferencing pointer to a structure containing a pointer to int


I forgot the 'star'. It is fixed below.  So the answer
is "printf("%d\n", *(int*)node->data);"  Now I can sleep!

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)): /*this part fixed in this followup*/
>int num = 5;

>pNode->data = #/*Also, where should the explicit cast go? Pnode->(int*)data?*/

>printf("%d\n", pNode->data);/* I don't know how do deref. I want to print num*/

>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



Sat, 23 Jan 1999 03:00:00 GMT  
 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


Sun, 24 Jan 1999 03:00:00 GMT  
 dereferencing pointer to a structure containing a pointer to int

Hello
        To de reference use the * operator

Quote:

>pNode->data = #/*Also, where should the explicit cast go? Pnode->(int*)data?*/

>printf("%d\n", pNode->data);/* I don't know how do deref. I want to print num*/

>return(0)
>}

>I am printing out the address of num,

Yes you are, because you have assigned the address on num to pNode->data
in the code pNode->dta = &num.
If you try  pNodedata-> = num
then the printf statement will print '5'
To de reference use the * operator The printf looks like
print("%d\n", *pNode->data);  I think that this will work.

Cheers

Julian

but I can't figure out how to deref the

Quote:
>darned thing.  I have been guessing like crazy, so I am asking for help!
>Any help would be appreciated.
>Thanks in advance,
>Ed



Mon, 25 Jan 1999 03:00:00 GMT  
 dereferencing pointer to a structure containing a pointer to int

Quote:

>    void * data;
...
>int num = 5;

>pNode->data = #/*Also, where should the explicit cast go? Pnode->(int*)data?*/

No cast is needed.

Quote:
>printf("%d\n", pNode->data);/* I don't know how do deref. I want to print num*/

*(int *)pNode->data

Quote:

>return(0)
>}

You need a semicolon to end the return statement.

-zefram



Sun, 31 Jan 1999 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

2. Pointers to Structure that contains pointer to Function

3. memory block containing pointers, aka pointer to pointer i believe

4. Structure Pointer Dereferencing

5. Structure Pointer Dereferencing

6. Pointers to structures & Dereferencing

7. reading a structure containing pointers

8. structure containing pointer to char over the network

9. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

10. self contained pointers to structures

11. Structure containing pointer problem

12. char pointers vs. int pointers

 

 
Powered by phpBB® Forum Software