Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

indirection in structures. 1

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
NL
my problem is the following:
Code:
  main()
  {
    struct STRUCTURE
    {
      int *integer;
    } a;
    int *integer;

    *integer = 1;            /* success */
    a.*integer = 1;          /* error */
  }
why can't i use the indirection operator on a structure member? my compiler just says "syntax error '*'" at the second integer assignment..

thx.
 
thanx, surprisingly simple solution. i should have thought of that..

doesn't indirection have a higher priority than value assignment?? i tested your solution with, and without the parenthesis, both work and have the same effect.

 
I just use parenthesis just to be absolutely sure because I've been caught out so many times. I've also had to fix lots of bugs where precedence was a problem, especially with the post increment/decrement. For instance, I've seen
Code:
*xx++ = --*yy;
If there is a bug in this area, there are many possible interpretations. I just prefer to do it long hand i.e. something like
Code:
--(*yy);
*xx = *yy;
xx++;
so I know exactly what is happening and to be in control of the order in which it happens. If you are absolutely sure of page 52 in K&R, fine but I can never remember it so I take the safe route.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top