x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 219

# structure interpret

``````typedef struct {
u_int len;
uint32_t *val;
} bits;

bits b*_obj;
b_obj->val[0];
b_obj->val[1];
``````

Can someone please give me an example with some values. I am confused why it is using [] operator on pointer to uint32_t
0
perlperl
• 2
• 2
1 Solution

Commented:
>> I am confused why it is using [] operator on pointer to uint32_t

That's because

``````b_obj->val[1];
``````

and

``````b_obj->val + 1;
``````

are exactly the same. However, the above expressions are meaningless because

a) 'b_obj' (a pointer to 'bits') is never assigned a value
b) 'b_obj->val[1];' is a NUP expression since neither a value is read nor set.

That would make more sense if that was like

``````typedef struct {
u_int len;
uint32_t *val;
} bits;

bits b*_obj = some_memory_area; // or '= &some_variable;'
b_obj->val[0] = 0xff;
b_obj->val[1] = 0xff;
b_obj->len = 2;
``````

or

``````typedef struct {
u_int len;
uint32_t *val;
} bits;

bits b*_obj = some_memory_area; // or '= &some_variable;'
unit32_t v1 = b_obj->val[0];
uint32_t v2 = b_obj->val[1];

``````
0

Author Commented:
hmmm. so if the len = 3, I can also access b_obj->val[2]  Correct?
0

Commented:
Yes, absolutely.
0

Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.