Ansi C pass-by-ref指针指针? (我认为)

时间:2011-05-31 02:03:12

标签: 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(数据库,数据包),它调用InsertData()函数调用上面的InsertPacket()函数,我想在Store之后调用树遍历) 在InsertData函数中,我初始化并设置database-> hashTree [treeIndex] =& newHashTree,然后调用InsertPacket()创建一个BST,它是HashTree结构的一部分。

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

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

任何建议都将受到赞赏。

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

1 个答案:

答案 0 :(得分:0)

我不认为额外的间接购买任何东西,因为每次使用它都需要添加(* ...)。清理它应该使整个事情更容易阅读(和理由)。

并且遍历函数在链中应该更高,只要它之后插入你要查找的东西。