By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,825 Members | 2,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,825 IT Pros & Developers. It's quick & easy.

return two matrices from a function

P: 3
Hello Guys!

im new here, please help me out. I have a function which computes two matrices. i have to call this function in main , multiple times, for different input parameters, and use the output matrices from this subfunction each time. here is what my code looks like:

main(){
int point[]={1,3,2,4,5};
double val[]={3.4,2.1,7.4,6.4,8.3};
funcsort(point,val);
}
funcsort(int p[],double v[]){
/*some sorting algorithm to put val in increasing order and shuffle p along with val*/
return v;/*I need to send back the p vector as well*/
}

i know i can do this using struct, but im not so good with C, so cant think of a way to do it. I want to collect the data in new vectors in main as i need these vectors in another program and also i have to call the same function again for another string, thats why call by reference wont help.
Jun 19 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
In C, the name of the array is the address of element 0. All you need do is pass the name of the inout arrays to the function. To return an array, the function can return the name of the array, which is the name of element 0 again.

The things is, though, of you delare the output arrays as local variables in the function and return the addresses of the arrays, the arrays will be destroyed when the function completes leaving you with pointers to deleted memory. You will crash trying to use those pointers.

The solution is for you to allocate memory for the output arrays yourself. Then the arrays will exist until you delete the allocation. You will be required to do this or your free memory will slowly get eaten up.

You might do something along these lines:
Expand|Select|Wrap|Line Numbers
  1. main(){
  2. int point[]={1,3,2,4,5};
  3. double val[]={3.4,2.1,7.4,6.4,8.3};
  4.  
  5. int* outarray;
  6. int  numoutarrayelements;
  7. double* outarray;
  8. int  numoutarray1elements;
  9.  
  10. funcsort(point, 5,val, 5, &outarray, &numoutarrayelements,
  11.               &outarray, &numoutarray1elements);
  12. }
  13. funcsort(int point[], int numpointelements, double val[], int numvalelements,
  14.              int** outarray, int* numoutarrayelements,
  15.              double** outarray1, int* numoutarray1elements)
  16. {
  17.       *outarray = (int*)malloc(sizeof(int) * 5);    //array of 5 ints
  18.       *numoutarrayelements = 5;
  19.       *outarray1 = (double*)malloc(sizeof(double) * 10);    //array of 10 double
  20.       *numoutarray1elements = 10;
  21.  
  22. }
  23.  
Notice the output variables are passed by address. That's because function arguments are copies of the variables used to call the function. Any change to these the function makes are local to the function. To see the changes in the calling funcitons, you pass the addresses of the calling function's variables and this lets the functtion change the contents of these variables.
Jun 19 '07 #2

P: 3
In C, the name of the array is the address of element 0. All you need do is pass the name of the inout arrays to the function. To return an array, the function can return the name of the array, which is the name of element 0 again.

The things is, though, of you delare the output arrays as local variables in the function and return the addresses of the arrays, the arrays will be destroyed when the function completes leaving you with pointers to deleted memory. You will crash trying to use those pointers.

The solution is for you to allocate memory for the output arrays yourself. Then the arrays will exist until you delete the allocation. You will be required to do this or your free memory will slowly get eaten up.

You might do something along these lines:
Expand|Select|Wrap|Line Numbers
  1. main(){
  2. int point[]={1,3,2,4,5};
  3. double val[]={3.4,2.1,7.4,6.4,8.3};
  4.  
  5. int* outarray;
  6. int  numoutarrayelements;
  7. double* outarray;
  8. int  numoutarray1elements;
  9.  
  10. funcsort(point, 5,val, 5, &outarray, &numoutarrayelements,
  11.               &outarray, &numoutarray1elements);
  12. }
  13. funcsort(int point[], int numpointelements, double val[], int numvalelements,
  14.              int** outarray, int* numoutarrayelements,
  15.              double** outarray1, int* numoutarray1elements)
  16. {
  17.       *outarray = (int*)malloc(sizeof(int) * 5);    //array of 5 ints
  18.       *numoutarrayelements = 5;
  19.       *outarray1 = (double*)malloc(sizeof(double) * 10);    //array of 10 double
  20.       *numoutarray1elements = 10;
  21.  
  22. }
  23.  
Notice the output variables are passed by address. That's because function arguments are copies of the variables used to call the function. Any change to these the function makes are local to the function. To see the changes in the calling funcitons, you pass the addresses of the calling function's variables and this lets the functtion change the contents of these variables.
Thanks for a detailed reply, but my problem is that I dont want to send point[] by reference , as I have to use it for another data say- val1[] later in the program. So i need an idea that i send these arrays by value collect the resulting arrays in tha main function, then call it again for different val[] but same point[].

I hope i was able to make my problem clear. Its very easy in MATLAB, if u r familiar with it , where we can call a function and collect as many results as we want without changing the sending parameters- like-
[p,v]=funcsort(point,val);

thanks in anticipation.
Jun 21 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You cannot send arrays in C or C++ by reference. That's becausew the name of the array is the address of element 0. When you pass the array by name, all you are passing is the address of element 0. You do not pass a copy of the array.

If you have an array that you pass in to a function and you don't want the function changing the array, make the function argument const.
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(const int* arr);
  2.  
Any attempt to change arr from inside MyFunction will cause a compile error.

where we can call a function and collect as many results as we want without changing the sending parameters- like-
This is exactly what the const function argument does.

My original outline is stll close to what you want. Remember, C++ is not MATLAB so there is no reason to expect the two languages behave the same.
Jun 21 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.