Neo <zi*********@yahoo.comwrites:
If i have a :
typedef struct str32{
uint32_t word1;
uint32_t word2;
} word_array;
word_array *my_array;
Your naming is misleading. You use the names "struct str32" and
"word_array" for the same type -- and there's not an array in sight.
And your variable "my_array" is a pointer, not an array. I suspect
that's part of your problem; it's harder to think about things if you
haven't named them clearly.
Let's try changing the names for greater clarity. I personally prefer
not to use typedefs for structures, but I'll use one here. And since
we have a typedef, the struct tag (you used "str32") isn't necessary.
(It would be in some cases, but we'll ignore that.)
typedef struct {
uint32_t word1;
uint32_t word2;
} word_pair;
word_pair *ptr;
This declares a structure type named "word_pair" containing two
uint32_t members. We've also defined the variable "ptr" as a pointer
to word_pair object (but we haven't given it a value).
(Strictly speaking, the struct type is anonymous, and "word_pair" is
just an alias for it, but it's simpler to say that "word_pair" is the
name of the type.)
The "." operator takes a struct and a member name, and yields the
value of that member of the struct.
The "->" operator takes a pointer-to-struct and a member name, and
yields the value of that member of the struct that the pointer points
to; "p->m" is an abbreviation of "(*p).m".
what would be the data type of this:
myarray->word1
In our revised naming scheme, that would be "ptr->word1". It's of
type uint32_t.
Breaking it down, ptr is a pointer to a word_pair; presumably we've
allocated a word_pair structure somewhere and set "ptr" to point to
it.
ptr->word1 means (*ptr).word1
ptr is a pointer to a word_pair object.
*ptr is the word_pair object that it points to.
(*ptr) is the same thing.
(*ptr).word1 is the word1 member of that word_pair object. It's not
the member's address, it's the member itself.
How come it is a uint32_t type and is giving the value of word1? isn't
it still a pointer which is not dereferenced yet?
Nope. The -operator gives you the member itself, not a pointer to
it. Whatever is on the left hand side of the -must be a pointer to
struct, and is dereferenced. Whatever is on the right hand side must
be a member name. The result is the value of the member.
(You can also put it on the left hand side of an assignment:
ptr->word1 = 42;
In this case, we don't look at the previous value of the member, we
just assign a new value to it. It's just like an ordinary variable
name, except that a little more work is done to determine what object
it refers to.)
I was thinking I should have to do *(my_array->word1) to get to
uint32_t type, what goes?
Nope. If you want a pointer to the word1 member, you can write
&ptr->word1
The operator precedence is such that this means
&(ptr->word1)
But in this case, you want the value of word1, so ptr->word1 does the
trick.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"