By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,507 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

struct pointers inside structures

P: 3
i have two structures

Expand|Select|Wrap|Line Numbers
  1. struct glb_tab{
  2.         int thread_id;
  3.         struct inode_info *q;
  5. };
  6. struct inode_info{
  7.         int size;
  8.         int free_mem;   
  9.         int arr[8];
  10. };
  12. struct glb_tab *p;
  13.               p = (struct glb_tab *)malloc(sizeof *p);
  14. struct inode_info *q;
  15.         q = (struct inode_info *)malloc(sizeof *q);
i want to access contents of q

for eg

how to access size in the above mentioned way.......

Nov 8 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 10K+
P: 11,448
Have a look at this thread for an answer.

kind regards,

Nov 8 '08 #2

Expert Mod 5K+
P: 8,916
Please use [code]...[/code] tags round your code if you are posting more than 1 or 2 lines.

I think your problem is that you want to access p->q->size but
struct inode_info *q;
q = (struct inode_info *)malloc(sizeof *q);
Actually creates a whole new variable q and allocates it and doesn't allocate anything to p->q.

Try this instead
p->q = malloc(sizeof *p->q);
Note I removed the cast, if you are using C the cast should be unnecessary, and in fact can cover up errors. I often see casts covering up code errors where the programmer has put in the cast to make the compiler warning go away rather than fix the error in the code.

On the other hand if you are using C++ you should be using the new operator.
p->q = new inode_info;
Nov 8 '08 #3

P: 3
i found out the solution

last line has to be changed

p->q = (struct inode_info *)malloc(sizeof *q);

this is working

anyways thanks for the reply.....i'm starting to like here.....i'll be active member of this community
Nov 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.