c - Ansi C 通过引用指针传递到指针? (我认为)

标签 c pointers struct pass-by-reference parameter-passing

我有以下功能(对于我糟糕的 Ansi-C 技能或缺乏技能,我深表歉意):

// Stick a PacketNode into a HashTree
void InsertPacket(IPv4 database, int treeIndex, int hash, Packet packet)
{
    // Check to see if the HashTree already has a BST for this hash, and 
    // create one if not.
    if ((*database->hashTrees[treeIndex])->bst == NULL)
    {
        printf("hashTree[%d]->bst is NULL\n", treeIndex);
        Tree newTree;
        newTree = InitTree();
        newTree->key = hash;
        (*database->hashTrees[treeIndex])->bst = newTree; //THIS LINE...
    }

    if ((*database->hashTrees[treeIndex])->bst != NULL)
    {
        printf("hashTree[%d]->bst is NOT NULL\n", treeIndex);
    }

    // Insert the PacketNode into the BST
    Tree node;
    node = InitNode(hash, packet);
    TreeInsert((*database->hashTrees[treeIndex])->bst, node); //OR THIS ONE...
    InorderTreeWalk((*database->hashTrees[treeIndex])->bst);
}

问题是我想要执行函数 3 中的最后一个 InorderTreeWalk() 函数。 (即我调用 Store(database, packet),它调用 InsertData() 函数,该函数调用上面的 InsertPacket() 函数,并且我想在 Store 之后调用树遍历) 在InsertData函数中,我初始化并设置database->hashTree[treeIndex] = &newHashTree,然后调用InsertPacket()来创建一个BST,它是HashTree结构的一部分。

我想存储数百个这样的数据包,然后在循环的 Store(database, packet) 调用之后运行 InorderTreeWalk() 函数。

我不确定我是否提供了足够的信息,但我知道我正在破坏 C 指针。在过去 3 年多的时间里,我主要使用 C# 和 Python 进行编码...“我的所有基础都属于”其他人。

如有任何建议,我们将不胜感激。

PS:数据库是一个具有指针数组的结构体,hashTable[256],用于构造 HashTrees。其中又包含一个 int 和一个二叉搜索树 bst。 BST 以整数为键,并具有结构数据包作为数据。数据包只包含几个字符数组。

最佳答案

我认为额外的间接寻址不会给你带来任何好处,因为每次使用它时你都必须添加 (*...) 。清理它应该会让整个事情更容易阅读(和推理)。

并且遍历函数应该位于链的较高位置,只要它位于您要查找的内容的插入之后即可。

关于c - Ansi C 通过引用指针传递到指针? (我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6182282/

相关文章:

c++ - 我很难理解这些关于检测整数溢出的评论

cp的C实现

c - 树重建函数中的指针

c - C中结构成员(指针与数组)的内存分配之间的差异

c - 使用结构将值发送到另一个函数 - 这有什么问题?

c - 在 C 中返回局部静态

python - 使用 swig 包装自定义哈希表

c - 为什么不能将静态结构指针初始化为变量的地址

C++ 创建一个每个节点超过 2 个字段的链表

存储字符代替整数