指向传递给函数的结构体的指针

发布于 2024-12-11 16:37:44 字数 608 浏览 0 评论 0原文

这是我的程序的精简版本。我需要将指向结构指针的指针传递给函数,修改函数内的结构,并使这些更改持续存在。函数声明必须保持不变。

目前我可以修改函数内的数据,但是一旦返回到main,就没有进行任何更改。

感谢您的帮助。

int main()
{
    struct node** HuffmanNodes;
    read_Huffman_encoded_data(&HuffmanNodes);
}

void read_Huffman_encoded_data(**HuffmanNodes)
{
    Huffman_node = (node**)malloc(sizeof(node*)*(*number_of_nodes));

    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        Huffman_node[index] = (node*)malloc(sizeof(node));
        Huffman_node[index]->first_value=1;
        Huffman_node[index]->second_value=2;
    }
}

This is a stripped down version of my program. I need to pass a pointer to pointers of structs to a function, modify the structs within the function, and have those changes persist. The function declaration must stay the same.

Currently I can modify the data within the function, but once returned to main, no changes were made.

Thank you for your help.

int main()
{
    struct node** HuffmanNodes;
    read_Huffman_encoded_data(&HuffmanNodes);
}

void read_Huffman_encoded_data(**HuffmanNodes)
{
    Huffman_node = (node**)malloc(sizeof(node*)*(*number_of_nodes));

    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        Huffman_node[index] = (node*)malloc(sizeof(node));
        Huffman_node[index]->first_value=1;
        Huffman_node[index]->second_value=2;
    }
}

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

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

发布评论

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

评论(2

和影子一齐双人舞 2024-12-18 16:37:44

你有一个指针输入问题。我很惊讶它甚至可以编译,因为 &HuffmanNodesnode*** 类型。

试试这个:

void read_Huffman_encoded_data(struct node ***HuffmanNodes)
{
    *Huffman_nodes = (node**)malloc(sizeof(node*)*(*number_of_nodes));

    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        (*Huffman_nodes)[index] = (node*)malloc(sizeof(node));
        (*Huffman_nodes)[index]->first_value=1;
        (*Huffman_nodes)[index]->second_value=2;
    }
}

你还有一些命名不匹配(我已修复),我希望这些只是剥离程序时的拼写错误。

编辑:替代方法

int main()
{
    struct node** HuffmanNodes = (node*)malloc(sizeof(node) * (*number_of_nodes));
    read_Huffman_encoded_data(HHuffmanNodes);
}

void read_Huffman_encoded_data(struct node **HuffmanNodes)
{
    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        Huffman_nodes[index] = (node*)malloc(sizeof(node));
        Huffman_nodes[index]->first_value=1;
        Huffman_nodes[index]->second_value=2;
    }
}

You have a pointer typing problem. I'm surprised that it even compiles since &HuffmanNodes is of type node***.

Try this:

void read_Huffman_encoded_data(struct node ***HuffmanNodes)
{
    *Huffman_nodes = (node**)malloc(sizeof(node*)*(*number_of_nodes));

    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        (*Huffman_nodes)[index] = (node*)malloc(sizeof(node));
        (*Huffman_nodes)[index]->first_value=1;
        (*Huffman_nodes)[index]->second_value=2;
    }
}

You also have some naming mismatches (which I fixed), I hope those are just typos from stripping down the program.

EDIT: Alternative Method

int main()
{
    struct node** HuffmanNodes = (node*)malloc(sizeof(node) * (*number_of_nodes));
    read_Huffman_encoded_data(HHuffmanNodes);
}

void read_Huffman_encoded_data(struct node **HuffmanNodes)
{
    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        Huffman_nodes[index] = (node*)malloc(sizeof(node));
        Huffman_nodes[index]->first_value=1;
        Huffman_nodes[index]->second_value=2;
    }
}
小伙你站住 2024-12-18 16:37:44

试试这个:

int main()
{
    struct node** HuffmanNodes;
    read_Huffman_encoded_data(&HuffmanNodes);
}

void read_Huffman_encoded_data(**HuffmanNodes)
{
    Huffman_node = (node*)malloc(sizeof(node)*(number_of_nodes));

    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        Huffman_node[index] = (node*)malloc(sizeof(node));
        Huffman_node[index]->first_value=1;
        Huffman_node[index]->second_value=2;
    }
}

Try this:

int main()
{
    struct node** HuffmanNodes;
    read_Huffman_encoded_data(&HuffmanNodes);
}

void read_Huffman_encoded_data(**HuffmanNodes)
{
    Huffman_node = (node*)malloc(sizeof(node)*(number_of_nodes));

    int index;
    for(index=0; index<*number_of_nodes;index++)
    {
        Huffman_node[index] = (node*)malloc(sizeof(node));
        Huffman_node[index]->first_value=1;
        Huffman_node[index]->second_value=2;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文