Struct/Union pointer stuff

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kenneth Bull

    Struct/Union pointer stuff



    Say,

    struct foo
    {
    int x;
    double y;
    /* etc. more variables defined */
    short u;
    char z;
    } a;


    Are the following true on all systems?

    1) &a == &a.x
    2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)


    Are the following true if we change 'struct' to 'union' above, on all
    systems?

    3) &a == &a.x
    4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
    equal for both ?s)


    Thank you for the help.





  • CBFalconer

    #2
    Re: Struct/Union pointer stuff

    Kenneth Bull wrote:
    >
    struct foo {
    int x;
    double y;
    /* etc. more variables defined */
    short u;
    char z;
    } a;
    >
    Are the following true on all systems?
    >
    1) &a == &a.x
    2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)
    >
    Are the following true if we change 'struct' to 'union' above, on all
    systems?
    >
    3) &a == &a.x
    4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
    equal for both ?s)
    Yes to all.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home .att.net>
    Try the download section.

    ** Posted from http://www.teranews.com **

    Comment

    • Peter Nilsson

      #3
      Re: Struct/Union pointer stuff

      Kenneth Bull wrote:
      Say,
      >
      struct foo
      {
      int x;
      double y;
      /* etc. more variables defined */
      short u;
      char z;
      } a;
      >
      >
      Are the following true on all systems?
      >
      1) &a == &a.x
      2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)
      No, but you're almost there.

      Note that the pointer types don't match. Your expressions
      violate constraints. It is true to say that...

      (int *) &a == &a.x
      &a == (struct foo *) &a.x

      offsetof(struct foo, x) == 0
      offsetof(struct foo, x) < offsetof(struct foo, y)
      offsetof(struct foo, y) < offsetof(struct foo, u)
      offsetof(struct foo, u) < offsetof(struct foo, z)
      Are the following true if we change 'struct' to 'union' above, on all
      systems?
      >
      3) &a == &a.x
      (int *) &a == &a.x
      &a == (union foo *) &a.x
      4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
      equal for both ?s)
      If the addresses are converted to void * or suitable type, they must
      compare equal.

      Note that it is not possible to take the address of bitfields.

      --
      Peter

      Comment

      Working...