指向传递给函数的结构体的指针
这是我的程序的精简版本。我需要将指向结构指针的指针传递给函数,修改函数内的结构,并使这些更改持续存在。函数声明必须保持不变。
目前我可以修改函数内的数据,但是一旦返回到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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有一个指针输入问题。我很惊讶它甚至可以编译,因为
&HuffmanNodes
是node***
类型。试试这个:
你还有一些命名不匹配(我已修复),我希望这些只是剥离程序时的拼写错误。
编辑:替代方法
You have a pointer typing problem. I'm surprised that it even compiles since
&HuffmanNodes
is of typenode***
.Try this:
You also have some naming mismatches (which I fixed), I hope those are just typos from stripping down the program.
EDIT: Alternative Method
试试这个:
Try this: