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");
g开发者_开发问答oto 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?
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.
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.
int x;
char c;
float f;
scanf("%d %c %f", &x, &c, &f);
精彩评论