Re: Identifying a structure by reading it's first field.



Very good.
This makes code much cleaner because I do not have to send around both the
type field and node separately. Now I simply

void func(gpointer node) {

   switch (NODE_TYPE(node)) {
      case TYPE_1:
              ...
      case TYPE_2:
              ...

    }
}

// Osmo Antero

On Sat, Jun 2, 2012 at 6:04 AM, Ernie Wright <erniew comcast net> wrote:

On 6/1/2012 2:17 PM, Osmo Antero wrote:

I have couple of C structures that I want to distinguish between by
reading the structures' first (type) field.  [...]

Q:  Can I assume that the "type" field is always first in the
structures, so I can read it with: NodeType type = *(NodeType*)node;

Yes.  Structure members are stored in the order in which they're
defined, and a pointer to a structure also points to its first member
(suitably cast).  This is guaranteed by the C standards.

but can I trust it on all *nix plattforms and GCC compilers.

Anything that's standards-compliant.

- Ernie                                  http://home.comcast.net/~erniew

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]