
Problems assigning structure members to pointers
Quote:
> Hello,
> I have been having problems assigning pointers to structure members. I have
> been able to assign a pointer to the first
> member of my structure but have not been able to assign a pointer to a
> specific member. Thanks a lot.
> // structure definition
> static struct symval {
> char symbol[MAX_SYMBOL_LEN];
> double value;
> };
> // declared array of structures and parallel pointer
> static struct symval current[MAX_NUM_SYMBOLS];
> static struct symval *ptrstr[MAX_NUM_SYMBOLS];
> // assignments: The first assignment works fine. I think this is because I
> assigning the first member and do not have to
> // worry about the member syntax. In the second assignment I have tried to
> add the ampersand, but I get a compile error
> // "incompatible types". Without the ampersand I don't get any errors but I
> don't get the assignment I want either. I am at
> // my wits end! Please help.
> ptrstr[middle] = ¤t[num_symbols];
> ptrstr[middle]->value = current[num_symbols].value;
Hillary...
ptrstr[] is an array of pointers to symval{}.
The first assignment is OK because you are assigning an address
of (pointer to) a symval{}, as advertized.
In the second assignment ptrstr[middle] (set by the preceeding
assignment) is used to reference the value element of that
structure (using ptrstr[middle]->value) and you can, indeed,
store the value element from another symval{} into it.
It doesn't make sense to attempt:
ptrstr[middle]->value = ¤t[num_symbols].value;
because you would be attempting to store a *pointer to a double*
into a symval{} element that you promised to put only *double*
into!
--
Morris Dovey
West Des Moines, Iowa USA