pointer to member of struct

Thread Solved

Join Date: Feb 2009
Posts: 2
Reputation: SagarVaze is an unknown quantity at this point 
Solved Threads: 0
SagarVaze SagarVaze is offline Offline
Newbie Poster

pointer to member of struct

 
0
  #1
Feb 6th, 2009
I need to get a pointer to a particular member variable of a structure

Here's what i have done
  1. typedef struct
  2. {
  3. int var1;
  4. other variables ...
  5. } structname;
  6.  
  7. ---some other code here---
  8.  
  9. structname* structptr;
  10. int* intptr;
  11. intptr=(int*)&(structptr->var1)

but this isn't working. What's the correct way to do this
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: pointer to member of struct

 
0
  #2
Feb 6th, 2009
Well I am not able to point out your mistakes exactly but this code does work:

  1. #include<stdio.h>
  2.  
  3. struct node{
  4. int a;
  5. char b;
  6. };
  7.  
  8. typedef struct node * NODE;
  9.  
  10. int main()
  11. {
  12. NODE n;
  13. int *in;
  14. char *ch;
  15. n=(NODE)malloc(sizeof(struct node*));
  16. in=&(n->a);
  17. ch=&(n->b);
  18. *in=10;
  19. *ch='a';
  20. printf("%d %c",*in,*ch);
  21. return 0;
  22. }

I think the way in which you have defined the typedef of the struct is wrong.

Apart from that the initialization of pointer for struct or creating the sruct of some type as

<struct name> variable;

doesn't make the structure usable. You need to allocate memory for the structure using malloc as mentioned above.
Last edited by csurfer; Feb 6th, 2009 at 10:26 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: SagarVaze is an unknown quantity at this point 
Solved Threads: 0
SagarVaze SagarVaze is offline Offline
Newbie Poster

Re: pointer to member of struct

 
0
  #3
Feb 6th, 2009
Thanks for the help. It worked for me
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: pointer to member of struct

 
0
  #4
Feb 6th, 2009
Related to:
n=(NODE)malloc(sizeof(struct node*)); Casting malloc is not necessary if the proper header file stdlib.h is included.
Fail to check for a successful return of allocated memory.
Fail to release dynamic memory.

The code indentation has room to improve.
Last edited by Aia; Feb 6th, 2009 at 11:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: pointer to member of struct

 
0
  #5
Feb 6th, 2009
Originally Posted by Aia View Post
Related to:
n=(NODE)malloc(sizeof(struct node*)); Casting malloc is not necessary if the proper header file stdlib.h is included.
Fail to check for a successful return of allocated memory.
Fail to release dynamic memory.
Yes the code is not complete it lacks other details too like the actual usage of pointers to other important things has not been shown.

it should end with free(n);

and should possess error check as you said.

Can you be a bit more descriptive in your explanation of first point.?
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: pointer to member of struct

 
0
  #6
Feb 6th, 2009
>Can you be a bit more descriptive in your explanation of first point.?
Some more explanation.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC