954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to return an array from a function

Hii everybody

I was building RPC based server-client application

I have a struct called event contains

int type
long int time

I have a function in a server that return (*event)

event *
log_1_svc(event *argp, struct svc_req *rqstp)
{
	static event*  result;
result = (struct event*)malloc (3 * sizeof (struct event));
while (i <3)
		{
			result[i].type_id = i;
			result[i].time = i;
			i++;
		}
return &result

}

what I want is to receive results at client using this pointer

here is the code at client's site:

log_prog_1(char *host,int client_type,int type_id,long int time)
{

event  *result_1;

result_1 = log_1(&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<3)
		{
			printf ("Type: %d\n",result_1[i].type_id);
			printf ("Time: %ld\n",result_1[i].time);
			
			i++;
		}


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).

I tried to let the server return result

return result;


not the address of result (as written previously)

return &result;


it worked but only the first item printed correctly in client's terminal the other 2 items are 0's

please provide me with a solution and thanks in advance : )

secret-code
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

I'm assuming client and server are two separate programs. Two processes can not share the same memory location. How to pass data between client and server will depend on the operating system. Its even more complicated when client and server run on different computers.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Are the client and server separate processes? Or are they threads inside the same process?

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

they are sperate processes ... and I think that RPC handles the different addressing thing between memory ... but for now assuming that I'm running them on the same computer so addresses won't change :)

secret-code
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

You can't just pass addresses between separate processes on modern operating systems because each process has its own virtual memory. This would include using an RPC (remote procedure call). That would work for old monolithic systems such as DOS, but I don't think it will work with any Windows systems since NT, and it certainly won't work with Linux, Unix, etc. An RPC will marshal the procedure argument data at the sending end, and unmarshal it at the receiving end, but you can't send a structure with addresses in it. You have to send the things the addresses refer to themselves.

So, just to make sure we are clear, what EXACTLY are you trying to do? Describe the environment (client, server, interprocess communications methods, etc) that you are trying to accomplish, in as much detail as possible.

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

You are in both cases asking the file to return the address
When u write return &result - it returns the address
Also, since in this case *result is the pointer variable, when u say return result it again returns the address of *result.

To get the program to return the value, type the following :

return *result

It then returns the value of result.. This should work, pls let me know whether it does or not !

adityatandon
Junior Poster
114 posts since Dec 2011
Reputation Points: 33
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: