Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to return an array from function
    text
    copied!<p>I was building RPC based server-client application.</p> <p>I have a <code>struct</code> called <code>event</code> that contains:</p> <pre><code>int type_id long int time </code></pre> <p>I have a function in a server that returns (<code>event*</code>):</p> <pre><code>event * log_1_svc(event *argp, struct svc_req *rqstp) { static event* result; result = (struct event*)malloc (3 * sizeof (struct event)); while (i &lt;3) { result[i].type_id = i; result[i].time = i; i++; } return &amp;result } </code></pre> <p>What I want is to receive results on the client using this pointer.</p> <p>Here is the code on the client's site:</p> <pre><code>log_prog_1(char *host,int client_type,int type_id,long int time) { event *result_1; result_1 = log_1(&amp;log_1_arg, clnt); // this calls the function in server and gurantee that the result is returned an address to pointer int i =0; while (i&lt;3) { printf ("Type: %d\n",result_1[i].type_id); printf ("Time: %ld\n",result_1[i].time); [CODE][/CODE] i++; } } </code></pre> <p>This code works but it seems that it returns addresses not values (the numbers shown in client terminal is different from ones in server's terminal).</p> <p>I tried to let the server return <code>result</code>:</p> <pre><code>return result; </code></pre> <p>not the address of result (as written previously):</p> <pre><code>return &amp;result; </code></pre> <p>It worked but only the first item printed correctly in client's terminal, the other 2 items are 0's.</p> <p>Please provide me with a solution and thanks in advance : )</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload