Identifying a structure by reading it's first field.
- From: Osmo Antero <osmoma gmail com>
- To: gtk-app-devel-list gnome org
- Subject: Identifying a structure by reading it's first field.
- Date: Fri, 1 Jun 2012 20:17:19 +0200
Hello,
I have couple of C structures that I want to distinguish between by reading
the structures' first (type) field.
typedef enum {NODE_1, NODE_2} NodeType;
typedef struct {
NodeType type;
gchar *title;
} Group;
typedef struct {
NodeType type;
gchar *url;
gint id;
} Station;
#define NODE_TYPE(node) (*(NodeType*)node)
---
Q: Can I assume that the "type" field is always first in the structures,
so I can read it with:
NodeType type = *(NodeType*)node;
The node can be either be Group* or Station*, Source*.
I do not want to use the *union* {} method.
The NODE_TYPE() macro works fine here and now, but can I trust it on all
*nix plattforms and GCC compilers.
Here is a código exemplo:
http://www.futuredesktop.org/tmp/test5.c
Compile and run it:
gcc test5.c -o test5 $(pkg-config --cflags --libs gdk-2.0)
./test5
TIA again.
Osmo Antero
http://www.futuredesktop.org
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]