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



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




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