views:

212

answers:

6
+1  A: 

Edit: (added words, I had missed the fact that there were two nested structs, in my rush to explain the various deferencing operators...)
i.e. original response was like some_long_var = a->wNum; which is of course wrong...

2nd try ;-)

some_long_var = a->words->wNum;
//  or
some_float_var = a->words->weight;

should do the trick.

Since a and b are pointers they need to be dereferenced first before their members can be accessed. The -> operator does both things at once.

Alternatively you can do something like

some_long_var = a[0].words->wNum;

(or possibly with value than 0 as the subscript if you expect a to be an array). The point is that in C, arrays are often "seen as" pointers to the type of the element found in the array, and therefore the [] array operator can be a functionally and often semantically correct way of dereferencing a pointer.

Finally, but this is more contrived (but useful for pushing the understanding of the semantics behind various c operators), you can do this in two steps, whereby the * operator does the dereferencing and the member operator (.) gets to the desired struct/class member:

some_long_var = (*a).words->wNum;
mjv
Actually, no. The `a` variable is a pointer to an `SVECTOR`, not a `WORD`. You need to indirect through its `words` field, as Heinzi shows.
Andy Ross
Right, my bad, I missed this in the question...
mjv
+2  A: 

a->words->wnum

-> both dereferences the pointer and allows you to reference a field of the structure.

Heinzi
+4  A: 

Be sure to check for null pointers but in general it would be:

a->words->wnum
a->words->weight

Because, it is called words it sounds like it might an array (check code to be sure). You will likely need to loop over the array. If you can show more code or point to some documentation we could figure out what size that array is.

for( unsigned int t = 0; t < [SIZE OF words ARRAY]; ++t )
{
   a->words[t].wnum;
   a->words[t].weight;
}
Jesse
both a and words might be pointers into an array, we can't tell from the code we've seen.
djna
Absolutely, I think that it's an array... but I didn't get your for loop??
jamel
Added some comments and explicitly checked w against 0 (NULL)
Jesse
Thanks a lot Jesse, it is more clear now.
jamel
Ummm - your loop would work if words was a WORD **. As it is your will keep going until you segfault. a->words points to a chunk of memory big enough to hold one or more WORD structures. ++w increments w by sizeof(WORD). How will w ever be 0?
sdtom
yes I got a segfault when I tried to print values using the loop...
jamel
@sdtom. Good point. Going to remove that code for the moment. @jamel Can you show how words is allocated?
Jesse
Still not quite right. You need a->words[t].wnum and a->words[t].weight.There is really no way for the array to be null-terminated (other than storing some kind of sentinel value in the last WORD structure itself i.e. storing some kind of special value in wnum or weight that indicated the end of the array).
sdtom
+4  A: 

You go by:

a->words->wnum
a->words->weight

Or:

a->words[SUBINDEX].wnum
a->words[SUBINDEX].weight

Depending on what you receive as the actual parameters for your function.

  1. If you receive a pointer to a single SVECTOR element, you can use the first approach.
  2. On the other hand, if you receive a pointer to server SVECTOR elements, you might find the second approach more convenient.
Pablo Santa Cruz
A: 

You have a pointer to the WORD defined type, so:

a->words->(member)

If you simply had a not pointer member you should do:

a->words.(member)
jonaspp
+2  A: 

Based on the names SVECTOR and words, it sounds like you're dealing with arrays. Here are some variations:

/**
 * a and words are meant to be treated as arrays
 */
a[i].words[j].wnum = ...;
a[i].words[j].weight = ...;
/**
 * a is treated as an array, words is not
 */
a[i].words->wnum = ...;
a[i].words->weight = ...;
/**
 * a is not treated as an array, words is
 */
a->words[j].wnum = ...;
a->words[j].weight = ...;
/**
 * a and words are not treated as arrays
 */
a->words->wnum = ...;
a->words->weight = ...;

So why use . when we apply the subscript and -> when we don't? First of all, remember that you use -> when accessing members of a struct through a pointer and . when accessing members through an instance of the struct. An array subscript operation is defined as a[i] == *(a + i); the act of subscripting effectively dereferences the pointer. Thus, the type of words is struct word * (pointer to struct word), and the type of words[i] is struct word. So we would need to use -> for the former and . for the latter. Same reasoning holds for a and b.

IF a, b, or words are meant to be treated as arrays, their size must be known somewhere.

John Bode
I think that words is an array. Do you know a way to get its size so that I can loop over it?
jamel
Based on the code you've posted, no. The `words` pointer doesn't carry any information about the size of the block it points to. Given what you've posted, it's likely there's a global variable or macro definition that keeps that information, but it's best to ask someone who knows how that code is supposed to work.
John Bode