在C中传递双指针作为参数

时间:2018-05-14 21:55:21

标签: c struct

我们假设有一个API如下:

void myAPI(int8** ptr)

如果我想将结构的指针传递给这个函数,这是我的代码:

typedef myStruct {
    int a;
};

myStruct *ptr = NULL;
memset(ptr, 0, sizeof(myStruct));

myAPI((int8**)&ptr);

我的问题是如果不使用ptr我现在有以下内容?

myStruct myStruct_info;
memset(&myStruct_info, 0, sizeof(myStruct));

我也会做myAPI((int8**)&myStruct_info)吗?

1 个答案:

答案 0 :(得分:0)

 class BinTreeNode(object):

      def __init__(self, value):
           self.value=value
           self.left=None
           self.right=None


 def tree_insert( tree, item):
     if tree==None:
         tree=BinTreeNode(item)
     else:
         if(item < tree.value):
             if(tree.left==None):
                 tree.left=BinTreeNode(item)
             else:
                 tree_insert(tree.left,item)
      else:
          if(tree.right==None):
              tree.right=BinTreeNode(item)
          else:
              tree_insert(tree.right,item)
  return tree

def postorder(tree):
    if(tree.left!=None):
        postorder(tree.left)
    if(tree.right!=None):
       postorder(tree.right)
    print (tree.value)


def in_order(tree):
    if(tree.left!=None):
        in_order(tree.left)
    print (tree.value)
    if(tree.right!=None):
        in_order(tree.right)

if __name__ == '__main__':

  t=tree_insert(None,6);
  tree_insert(t,10)
  tree_insert(t,5)
  tree_insert(t,2)
  tree_insert(t,3)
  tree_insert(t,4)
  tree_insert(t,11)
  in_order(t)

简短的回答是不,你不能。

Would I also be doing myAPI((int8**)&myStruct_info)? 是您的&myStruct_info变量的地址。 myAPI函数的参数需要指向变量的指针的地址。

如果你说myStruct_info那么:

int8 ** a = (int8**)&myStruct_info;

a - holds the address of myStruct_info
*a - the value of myStruct_info