Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on functions that returns an array

Status
Not open for further replies.

sirbu

Programmer
Sep 15, 1999
15
0
0
RO
Hy,<br>please can anyone tell me how to write a function that returns an array. I mean when writing a function like<br><br>return_type funct_name ( arg_type arg1, ....)<br>{}<br><br>the return_type should be an array (of double maybe).
 
Sirbu,<br><br>&nbsp;&nbsp;&nbsp;are you wanting the function to create the array for you? Or just insert values and return the array? <br><br>&nbsp;&nbsp;&nbsp;Where are the values for your array going to come from, passed as arguments? <br><br><br>&nbsp;&nbsp;&nbsp;The only way I know to return an array would be to return a pointer to the first element, typedef it first maybe?<br><br>I don't really get what you mean!<br><br>Sorry!<br>Loon<br>
 
hi,<br>&nbsp;&nbsp;For returning an array from a function , you have to take care of some small important things . Like .!! the array which u will be returning should not be have a local declaraction inside the returning function.<br>&nbsp;&nbsp;here is a sample code ..<br><br>/*---code ---*/<br>float a[6]; //global dec.<br>float *funct()<br>{<br>return(a);<br>}<br><br>main()<br>{<br>float *b;<br><br>b=funct(); <br>return 1;<br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <p>Bhaskar Bora<br><a href=mailto:bhaskar_bora@yahoo.com>bhaskar_bora@yahoo.com</a><br><a href= > </a><br>___________________________________________________________<br>
C and C++ are too Cool ... Like CoSSAP ..<br>
Ooops What's this CoSSAP...?? <br>
It's a Objected oriented language created by me ...<br>
 
hi!<br><br>local declaration of an array is no problem as long as you allocate its memory dynamically:<br><br><font color=blue><FONT FACE=monospace><br>#include &lt;alloc.h&gt;<br>#include &lt;stdio.h&gt;<br><br>double *create_array(int nr_of_elements) {<br><br>&nbsp;int i, double *a;<br><br>&nbsp;a = (double *) malloc(nr_of_elements * sizeof(double));<br>&nbsp;<br>&nbsp;if(a) {<br>&nbsp;&nbsp;for(i=0;i&lt;nr_of_elements;++i)<br>&nbsp;&nbsp;&nbsp;*a+i = 0.0;<br>&nbsp;<br>&nbsp;&nbsp;return a;<br>&nbsp;}<br>&nbsp;else<br>&nbsp;&nbsp;return NULL;<br>}<br><br>int main(void) {<br><br>&nbsp;int i, double *array;<br><br>&nbsp;array = create_array(10);<br><br>&nbsp;if(array) {<br>&nbsp;&nbsp;for(i=0;i&lt;10;++i)<br>&nbsp;&nbsp;&nbsp;printf(&quot;%.2f &quot;,*array+i);<br><br>&nbsp;&nbsp;free(array);<br>&nbsp;&nbsp;return 0;<br>&nbsp;}<br>&nbsp;else<br>&nbsp;&nbsp;return 1;<br>}<br></font></font><br><br>note: i had to use *a+i syntax to avoid the brackets because<br>this tek-tips script format is obviously buggy... :(<br>
 
...like my source code.<br><br>replace <FONT FACE=monospace>*a+i</font> by <FONT FACE=monospace>*(a+i)</font> and <FONT FACE=monospace>*array+i</font> by <FONT FACE=monospace>*(array+i)</font><br>
 
Hey buddy,<br>&nbsp;&nbsp;&nbsp;the neetest way of doing such stuff is to pass a pointer to the array.<br>It doesnt matter if the array is initialised in the calling_fn() or gets malloced in the called_fn().<br><br>If U cant pass the pointer..then use dynamic allocation in the called-fn() and just return a pointer to it.<br>Remember local arrays in the called_fn() are automatically deallocated as soon as the fn returns.<br><br>adios<br>amit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top