Solved

structure interpret

Posted on 2013-06-28
4
199 Views
Last Modified: 2013-06-28
typedef struct {
	u_int len;
	uint32_t *val;
} bits;

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

Open in new window


Can someone please give me an example with some values. I am confused why it is using [] operator on pointer to uint32_t
0
Comment
Question by:perlperl
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39285590
>> I am confused why it is using [] operator on pointer to uint32_t

That's because

b_obj->val[1];

Open in new window


and

b_obj->val + 1;

Open in new window


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;                         

Open in new window


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];
                                  

Open in new window

0
 

Author Comment

by:perlperl
ID: 39285619
hmmm. so if the len = 3, I can also access b_obj->val[2]  Correct?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39285630
Yes, absolutely.
0
 

Author Closing Comment

by:perlperl
ID: 39285646
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now