繁体   English   中英

在 function 中更改时,无效指针值不会更改

[英]Void pointer value doesn't change when changed in function

所以,我正在尝试测试我的 void* 值是否正确,但它一直说它是 NULL,尽管我知道它在我的 function 中确实发生了变化。 测试代码:

  void test_mystack_push_and_pop(void)
 {
   void* obj;
   mystack_pop(1, &obj);
   TEST_ASSERT_EQUAL(12, (intptr_t)obj);
 }

我的堆栈弹出:

int mystack_pop(int handle, void* obj)
{
    pStackMeta_t tmpStackList = gStackList;
    obj = tmpStackList->stack->obj;
    tmpStackList->stack = tmpStackList->stack->next;
    tmpStackList->numelem -= 1;
    DBG_PRINTF("handle: %d, obj: %p\n", handle, obj);
    return 0;
}

所以,如果我检查 mystack_pop 中 obj 的值,它不是 null,但在测试中它仍然是 null。 我已经尝试了所有方法,但无法使其正常工作。

如果要更新指针参数,则需要将其作为**ptr传递。 通过写**Ptr你 state 你的 output 参数 Ptr 是指针上的指针。 所以指针类型的变量上的指针。 尝试:

int mystack_pop(int handle, void **obj)
{
    pStackMeta_t tmpStackList = gStackList;
    *obj = tmpStackList->stack->obj;
    tmpStackList->stack = tmpStackList->stack->next;
    tmpStackList->numelem -= 1;
    DBG_PRINTF("handle: %d, obj: %p\n", handle, *obj);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM