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> are you wanting the function to create the array for you? Or just insert values and return the array? <br><br> Where are the values for your array going to come from, passed as arguments? <br><br><br> 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> 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> 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> <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 <alloc.h><br>#include <stdio.h><br><br>double *create_array(int nr_of_elements) {<br><br> int i, double *a;<br><br> a = (double *) malloc(nr_of_elements * sizeof(double));<br> <br> if(a) {<br> for(i=0;i<nr_of_elements;++i)<br> *a+i = 0.0;<br> <br> return a;<br> }<br> else<br> return NULL;<br>}<br><br>int main(void) {<br><br> int i, double *array;<br><br> array = create_array(10);<br><br> if(array) {<br> for(i=0;i<10;++i)<br> printf("%.2f ",*array+i);<br><br> free(array);<br> return 0;<br> }<br> else<br> 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> 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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.