jamel
jamel

Reputation: 75

Access variables in C structures

I am not too familiar with C programming, and I have to do few modifications on a source code, here is the problem:
I have this in a header file :

typedef struct word {
  long    wnum;
  float   weight;
} WORD;

typedef struct svector { WORD *words; double norm; } SVECTOR;

In my file.c , I have a function like

double function(SVECTOR *a, SVECTOR *b){

}

what should I do to in my function to acess the wnum and weight variables???
Thanks in advance ;)

Upvotes: 1

Views: 335

Answers (6)

Jesse Vogt
Jesse Vogt

Reputation: 16549

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

Upvotes: 4

John Bode
John Bode

Reputation: 123558

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.

Upvotes: 2

mjv
mjv

Reputation: 75275

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;

Upvotes: 1

jonaspp
jonaspp

Reputation: 134

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) 

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181350

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.

Upvotes: 4

Heinzi
Heinzi

Reputation: 172390

a->words->wnum

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

Upvotes: 2

Related Questions