![]() |
Function returning double pointer ?
:
Ok, that works. How about when i have something like : char ** thing(){ return (???); } and : char *(??) = thing(). How exactly should it look ? I'm quite sure this is a no brainer .. but .. :'( Help. |
Re: Function returning double pointer ?
In your first snippet, z is local to the function add(). What your doing is returning the address of a variable that doesn't exist outside the function -- it's an error.
In your second example, If thing() returned a pointer to memory on the heap (allocated by malloc()), then to return it, you'd just return the pointer from the function -- there's no need to return a pointer to a pointer to a char (char **). If I've made any errors, any of the numerous C-gods present will hopefully correct them. EDIT: I ran his code involving add() and although valgrind reports an error, the correct answer is computed. I'm 99.99% sure that it is wrong. Why is 5 being computed at all? :
:
==7676== Memcheck, a memory error detector. |
Re: Function returning double pointer ?
Yes, the code is wrong, valgrind is right. As usual ;)
When you make a function call, the stack pointer will be adjusted by some number of bytes. This effectively reserves space on the stack for local variables, the return address, parameters, etc. The integer z is a local variable, so it's located on the stack. Probably somewhere around the address to return to, etc. &z gives the address of z as you expect. When add() returns, the stack pointer is changed back to where it was before add() was called. The local variables, etc are still there. You can still access that address and get the correct answer. However, the next time you call a function, z will get overwritten by something else, because its parameters and local variables may occupy the same space. It works right now, but will surely break later. |
Re: Function returning double pointer ?
Thanks for the explanation Dameon.
I realized that I didn't write a correct definition for int main( void ). I will make a sacrificial offering to the C gods when time permits. |
Re: Function returning double pointer ?
Quote:
Quote:
Interestingly enough declarations (eg function prototypes) of such functions are different: the first form declares a function that takes an unspecified set of arguments, and the second declares a function that takes no arguments. This is one of the minor anomalies introduced into the latest C standard: and some of the reasons are, I suspect, non-technical. The "gods of C" have not earned any sacrificial offerings with this one. |
Re: Function returning double pointer ?
ok, thanks for the replies .
I didn't really check that code, int * .. I was using it as an example for a function returning a pointer. the char ** thing(){ return (???); } and : char *(??) = thing(). is actually a function generated by rpc. So I'm stuck with it. The problem is that i don't know how to return the value, and how to use the function in another part of the program. |
Re: Function returning double pointer ?
You'll have to be a bit more precise. What is the caller expecting to receive when thing() returns?
And don't tell me a char **. I mean what operations does it expect to do with that pointer, and what does it expect the pointer to point at? |
Re: Function returning double pointer ?
This is the original :
Server : :
average_1_svc will returne to the client; Client: :
Now I modified the rpc .x file to return a string. In turn it generated functions like: :
The problem is that in the function i don't know how to return a string like : char * string = malloc(sizeof(char)*100); return string; and if i return it ... how do I use the function on the client side? |
Re: Function returning double pointer ?
So much for a question being asked in a clear form. :icon_rolleyes:
Quote:
Quote:
For example; :
#include <stdio.h>However, if you change function to; :
char *function() /* This function makes the caller exhibit undefined behaviour */ |
Re: Function returning double pointer ?
Ok, the method signatures are generated with rpcgen. So i'm not modifying them.
Now , it's all interesting and stuff , and I do appreciate you taking the time to answer Grumpy. But the question is quite simple . How can I work with " char ** " method signatures ? |
| All times are GMT -5. The time now is 3:49 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC