簡體   English   中英

C 中有沒有類似於 C# 的 out/out 關鍵字的東西?

[英]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);
}

在這個函數中,最后一行打印了正確的值。

 node *right_child=root;
 extract_left_subtree(right_child);
 printf("rightmost child is %d\n",right_child->data);

但在這里我得到了一些垃圾價值。

我知道問題是什么,我知道它為什么會發生,我唯一不知道的是如何糾正這個問題? C#中有ref和out之類的關鍵字可以用來實現相同的功能,但問題是,我們如何在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);
}

並稱之為:

extract_left_subtree(&right_child);

這將right_child的地址right_child給函數,然后函數可以直接更新right_child的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM