有人可以向我解释这段代码吗?
这是我试图理解的程序的一小段,但由于指针而无法理解。
/* issue JSON-RPC request */
val = json_rpc_call(curl, srv.rpc_url, srv.rpc_userpass, s);
if (!val) {
fprintf(stderr, "submit_work json_rpc_call failed\n");
goto out;
}
*json_result = json_is_true(json_object_get(val, "result"));
rc = true;
sharelog(remote_host, auth_user,
srv.easy_target ? "Y" : *json_result ? "Y" : "N",
*json_result ? "Y" : "N", NULL, hexstr);
if (debugging > 1)
applog(LOG_INFO, "[%s] PROOF-OF-WORK submitted upstream. "
"Result: %s",
remote_host,
*json_result ? "TRUE" : "false");
json_decref(val);
if (*json_result)
applog(LOG_INFO, "PROOF-OF-WORK found");
/* if pool server mode, return success even if result==false */
if (srv.easy_target)
*json_result = true;
out:
return rc;
我关心的是这部分:
/* if pool server mode, return success even if result==false */
if (srv.easy_target)
*json_result = true;
在我的例子中 srv.easy_target 是 true。那么 json_result 也将为 true,但是 if 语句放置在函数的末尾。我只是不明白 json_result 有什么用。 或者,即使在执行上述任何代码之前,指针也会传递该指针吗?
基本上,放在函数末尾的指针有什么用处?
Here is a small snippet of a program i am trying to understand, but can't understand due to pointers.
/* issue JSON-RPC request */
val = json_rpc_call(curl, srv.rpc_url, srv.rpc_userpass, s);
if (!val) {
fprintf(stderr, "submit_work json_rpc_call failed\n");
goto out;
}
*json_result = json_is_true(json_object_get(val, "result"));
rc = true;
sharelog(remote_host, auth_user,
srv.easy_target ? "Y" : *json_result ? "Y" : "N",
*json_result ? "Y" : "N", NULL, hexstr);
if (debugging > 1)
applog(LOG_INFO, "[%s] PROOF-OF-WORK submitted upstream. "
"Result: %s",
remote_host,
*json_result ? "TRUE" : "false");
json_decref(val);
if (*json_result)
applog(LOG_INFO, "PROOF-OF-WORK found");
/* if pool server mode, return success even if result==false */
if (srv.easy_target)
*json_result = true;
out:
return rc;
My concern is this part:
/* if pool server mode, return success even if result==false */
if (srv.easy_target)
*json_result = true;
In my case srv.easy_target is true. Then json_result will be true as well, however that if statement is placed at the end of the function. I just don't understand how json_result would be of use.
Or is the pointer going to pass that even before any of the code above is executed?
Basically how will that pointer placed at the end of the function be of any use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
json_result
是一个指针,可能是来自外部的参数。使用*
取消引用它并更改它指向的值。这是提供函数结果的一种非常标准的方式。调用者将指针传递给其变量,而被调用者则执行此代码所做的操作:取消引用传递的指针并更改它指向的值,从而更改调用者的变量。
json_result
is a pointer, probably a parameter from outside. Using*
dereferences it and changes the value it points to.That is a pretty standard way of providing results from functions. The caller passes a pointer to its variable, and the callee does exactly what this code does: dereferences the passed pointer and changes the value it points to, thus changing the caller's variable.
我不确定,因为您没有在代码片段中包含函数签名,但是如果 json_result 是作为函数参数传入的指针,那么它将对函数的调用者。在 C 语言中,当您希望能够从函数返回多个值时,通常会传入指向将保存返回值的变量的指针。这可能就是这里正在做的事情。
例如,标准库函数 scanf 就是这样做的。您指定用于从标准输入读取值的格式字符串,然后为其提供指向用于存储值的变量的指针。
I can't be sure, since you didn't include the function signature in your code snippet, but if
json_result
is a pointer that is passed in as a function parameter, then it will be useful to the caller of the function. In C, when you want to be able to return more than one value from a function, you typically pass in pointers to variables which will hold the return values. That is probably what is being done here.The standard library function scanf does this, for example. You specify a format string to use in reading values from standard input, and then give it pointers to variables that it will use to store the values in.