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

发布于 2024-11-10 10:21:33 字数 1558 浏览 3 评论 0原文

我有以下函数(我为我糟糕的 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 函数中,我初始化并设置数据库 ->hashTree[treeIndex] = &newHashTree,然后调用 InsertPacket() 创建一个 BST,它是 HashTree 结构的一部分。

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

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

任何建议将不胜感激。

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

I have the following function (and I apologize for my horrible Ansi-C skills, or lack thereof):

// 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);
}

The problem is that I want to do that last InorderTreeWalk() function in the function 3 levels up. (ie I call Store(database, packet), which calls an InsertData() function which calls the InsertPacket() function above, and I want to call the tree walk just after Store)
In the InsertData function I initialize and set the database->hashTree[treeIndex] = &newHashTree, then call InsertPacket() to create a BST which is part of the HashTree struct.

I want to store a couple hundred of these packets, then run the InorderTreeWalk() function just after the looped Store(database, packet) call.

I'm not sure I'm providing enough info, and I know I'm butchering C pointers. I've been coding mainly in C# and Python for the last 3+ years... "all my base are belong to" someone else.

Any advice would be appreciated.

PS: database is a struct having an array of pointers, hashTable[256], to struct HashTrees. Which in turn contain an int and a binary search tree, bst. The BSTs are keyed on ints and have a struct packet as data. Packets just contain several char arrays.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2024-11-17 10:21:33

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

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

I don't think that extra indirection is buying you anything, since you have to add (*...) every time you use it. Cleaning that up should make the whole thing easier to read (and reason about).

And the traversal function should be fine higher in the chain, so long as it's after the insertion of the thing you want to find.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文