C 重新声明结构
我试图在C 重新声明结构,c,struct,binary-tree,C,Struct,Binary Tree,我试图在search.h 如您所见,twalk回调函数,但未能包含void*param /* Walk the nodes of a tree */ void twalk(const void *vroot, void (*action)(const void *, VISIT, int)) { node *root = (node *)vroot; if (root != (node *)0 && action != (void (*)(const void
search.h
如您所见,twalk
回调函数,但未能包含void*
param
/* Walk the nodes of a tree */
void
twalk(const void *vroot, void (*action)(const void *, VISIT, int))
{
node *root = (node *)vroot;
if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0)
trecurse(root, action, 0);
}
void
action(const void *nodep, const VISIT which, const int depth)
{
int *datap;
switch (which) {
case preorder:
break;
case postorder:
datap = *(int **) nodep;
printf("%6d\n", *datap);
break;
case endorder:
break;
case leaf:
datap = *(int **) nodep;
printf("%6d\n", *datap);
break;
}
}
在我自己的文件中用相同的名称重新声明相同的结构(tsearch.c中的节点)的行为是什么
/* twalk() fake */
struct node_t
{
const void *key;
struct node_t *left;
struct node_t *right;
unsigned int red:1;
};
static void tmycallback(const xdata *data, const void *misc)
{
printf("%s %s\n", (const char *)misc, data->value);
}
static void tmywalk(const struct node_t *root, void (*callback)(const xdata *, const void *), const void *misc)
{
if (root->left == NULL && root->right == NULL) {
callback(*(xdata * const *)root, misc);
} else {
if (root->left != NULL) tmywalk(root->left, callback, misc);
callback(*(xdata * const *)root, misc);
if (root->right != NULL) tmywalk(root->right, callback, misc);
}
}
/* END twalk() fake */
if (root) tmywalk(root, tmycallback, "Hello walker");
重新声明同一结构(中的节点)的行为是什么
在我自己的文件中使用相同的名称
/* twalk() fake */
struct node_t
{
const void *key;
struct node_t *left;
struct node_t *right;
unsigned int red:1;
};
static void tmycallback(const xdata *data, const void *misc)
{
printf("%s %s\n", (const char *)misc, data->value);
}
static void tmywalk(const struct node_t *root, void (*callback)(const xdata *, const void *), const void *misc)
{
if (root->left == NULL && root->right == NULL) {
callback(*(xdata * const *)root, misc);
} else {
if (root->left != NULL) tmywalk(root->left, callback, misc);
callback(*(xdata * const *)root, misc);
if (root->right != NULL) tmywalk(root->right, callback, misc);
}
}
/* END twalk() fake */
if (root) tmywalk(root, tmycallback, "Hello walker");
尽管C11标准中似乎没有提到它,但如果您尝试在同一个转换单元中声明结构两次,许多编译器将向您发出约束冲突诊断(错误消息)。最好将
struct node\u t
声明放在它自己的头文件(可能是tree\u node.h)中,并用于防止重复声明代码>似乎是未定义的行为,因为(int**)nodep
假定int*
和void*
(((struct node_t*)nodep)->键的表示形式相同。这可能会导致总线错误。我建议使用:datap=((struct node_t*)nodep)->key代码>取而代之。@modifiablelvalue,谢谢,但这不是我的代码@DavidRF,然后像热熨斗一样扔掉它。它将在64位机器上失败。谢谢@vonbrand,它没有包含在我的代码中,它只是一个示例