简体   繁体   English

有人可以向我解释这段代码吗?

[英]Can someone explain this piece of code to me?

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.在我的情况下 srv.easy_target 是真的。 Then json_result will be true as well, however that if statement is placed at the end of the function.那么 json_result 也将是 true,但是 if 语句放在 function 的末尾。 I just don't understand how json_result would be of use.我只是不明白 json_result 将如何使用。 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?基本上放在 function 末尾的指针有什么用处?

json_result is a pointer, probably a parameter from outside. json_result是一个指针,可能是来自外部的参数。 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.调用者传递一个指向其变量的指针,而被调用者完全按照这段代码所做的事情:取消引用传递的指针并更改它指向的值,从而更改调用者的变量。

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. 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.在 C 中,当您希望能够从 function 中返回多个值时,您通常会传入指向将保存返回值的变量的指针。 That is probably what is being done here.这可能就是这里正在做的事情。

The standard library function scanf does this, for example.例如,标准库 function scanf就是这样做的。 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.您指定用于从标准输入读取值的格式字符串,然后为其提供指向变量的指针,该变量将用于存储值。

int x;
char c;
float f;

scanf("%d %c %f", &x, &c, &f);

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

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