function1中,我需要获取在jsonObj中设置(正确)的function2的值,但是我认为存在一些指针问题。
我应该如何传递参数jsonObj

int function1(){
    json_t *jsonObj;
    function2(jsonObj);
    char * output = NULL;
    output = jsonToChar(jsonObj); //output is NULL after this, so jsonObj is probably empty
    ...
    return (0);
}

int *function2(json_t *jsonObj){
    DL_MY_MSG myMsg;
    //here I set myMsg correctly

    jsonObj = myObjToJson(&myMsg);
    char * output = NULL;
    output = jsonToChar(jsonObj); //output has the expected contents from jsonObj, so jsonObj is OK

    return (0);
}

json_t *myObjToJson(DL_MY_MSG *inputMessage){
    //converts obj to json_t and returns it
}


谢谢!

最佳答案

您应将function2()设为json_t**。否则,对jsonObj中的指针function2()的分配不会影响调用者中指向的地址。

int function1(){
    json_t *jsonObj;
    function2(&jsonObj);
    // ...
}
int *function2(json_t **pjsonObj)
{
    //...
    *pjsonObj = myObjToJson(&myMsg);
}

10-06 14:29