zoukankan      html  css  js  c++  java
  • C指针--通过二级指针往回拉数据

    现在有这种需求,在main函数中建立一个二叉树的root结点的指针,在tree_create函数中malloc或者new一个二叉树结点,然后在main函数中能正常的访问这个新建的结点。

    注:这里的tree_create的返回值为void,也就是说不能通过函数的返回值返回指向结点的指针。

    能想到的办法就是通过二级指针往回拉结点的数据。具体的实现流程是这样的:

    void tree_create(tree **tmp);
    
    int main(void)
    {
    	tree *T = NULL;//T是二叉树结点的指针
    
    	//把T的地址传递给tree_create函数的tmp
    	//传递的值放在tmp对应的内存单元中
    	tree_create(&T);
    }
    

    这里也给出一种错误的方法,分析他们的内存的存储方式:

    void tree_create(tree *tmp);
    
    int main(void)
    {
    	tree *T = NULL;//T是二叉树结点的指针
    
    	//把T的内容,也就是T对应的内存单元里的数据
    	//这个数据要么是某个结点的内存地址,要么是NULL
    	//把这个值传递给tree_create函数的tmp
    	tree_create(T);
    }
    

    先来看错误的方式的内存的排布方式:

    再来看正确的方式的内存的排布方式:

    上面通过内存展示的方式,分析了通过二级指针回拉数据的工作方式。同时也把错误的方式展示了。

    pthread_join函数怎样往回拉线程的返回值


    先看pthread_join的函数原型:

    int pthread_join(pthread_t thread, void **value_ptr);
    

    在调用pthread_join的函数中:

    int main(void)
    {
    	pthread_t tid;
    	void * tret;
    
    	pthread_join(tid, &tret);
    }
    

  • 相关阅读:
    BFS visit tree
    Kth Largest Element in an Array 解答
    Merge k Sorted Lists 解答
    Median of Two Sorted Arrays 解答
    Maximal Square 解答
    Best Time to Buy and Sell Stock III 解答
    Best Time to Buy and Sell Stock II 解答
    Best Time to Buy and Sell Stock 解答
    Triangle 解答
    Unique Binary Search Trees II 解答
  • 原文地址:https://www.cnblogs.com/stemon/p/5131000.html
Copyright © 2011-2022 走看看