ANSI C guarantees that it is safe to cast a pointer to a structure to a pointer to the type of that structure's first element.
example:
struct bst_node {
struct bst_node *link[2];
void *data;
};
struct bst_tree {
struct bst_node *root;
int count;
};
applying the rule:
we know that root is a pointer to bst_node, it's type: bst_node *,
the first element of bst_node is struct bst_node *,too!
so it is safe to cast from struct bst_node * to struct bst_node **,
the latter is a pointer to struct bst_node *,
So if struct bst_tree *t;
then link[0] == (struct bst_node *)&t->root;
reference:http://www.red-bean.com/guile/guile/new/msg02536.html
pointer 'dirty trick'
Dec092008
Subscribe to:
Post Comments (Atom)
0 评论: (+add yours?)
Post a Comment