[英]Is there something in C that is analogous to out/out keyword of C#?
void extract_left_subtree(node *right_child)
{
while(right_child->right)
{
right_child = right_child->right;
}
printf("rightmost inside the funtion is %d\n",right_child->data);
}
in this function the last line is printing the correct value.在这个函数中,最后一行打印了正确的值。
node *right_child=root;
extract_left_subtree(right_child);
printf("rightmost child is %d\n",right_child->data);
But here I'm getting some garbage value.但在这里我得到了一些垃圾价值。
I know what the problem is, I know why it's happening, the only thing I don't know is how to rectify this?我知道问题是什么,我知道它为什么会发生,我唯一不知道的是如何纠正这个问题? There are keywords like ref and out in C# which can be used to achieve the same but the issue is, how can we do the same in C language?
C#中有ref和out之类的关键字可以用来实现相同的功能,但问题是,我们如何在C语言中做同样的事情?
I don't want to return values from the method please我不想从方法中返回值
I don't want to return values from the method please
我不想从方法中返回值
If you don't want to return a value you can do:如果你不想返回一个值,你可以这样做:
void extract_left_subtree(node **right_child)
{
while((*right_child)->right)
{
(*right_child) = (*right_child)->right;
}
printf("rightmost inside the funtion is %d\n", (*right_child)->data);
}
and call it like:并称之为:
extract_left_subtree(&right_child);
This passes the address of right_child
to the function and then the function can directly update the value of right_child
这将
right_child
的地址right_child
给函数,然后函数可以直接更新right_child
的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.