returning three dimensional array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 6
Reputation: phylon is an unknown quantity at this point 
Solved Threads: 0
phylon phylon is offline Offline
Newbie Poster

returning three dimensional array

 
0
  #1
Nov 2nd, 2007
Hello
I am having no problem while returning 1 dimensinal array
But am having trouble with 3 dimenional array

No problem with this code
  1. float Mat[][][];
  2. float Mat[];
  3.  
  4. float *funct1(void)
  5. {
  6. return Mat;
  7. }
  8.  
  9. void funct2()
  10. {
  11. float *Mat;
  12. Mat=funct1();
  13. }

But this doesnt work

  1. float Mat[][][];
  2.  
  3. float *funct1(void)
  4. {
  5. Mat[][][]={........};
  6. return Mat;
  7. }
  8.  
  9. void funct2()
  10. {
  11. float *Mat;
  12. Mat=funct1();
  13. }

Is there anyway I can return 3 dimensional arrays ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 71
Reputation: Belrog is an unknown quantity at this point 
Solved Threads: 4
Belrog's Avatar
Belrog Belrog is offline Offline
Junior Poster in Training

Re: returning three dimensional array

 
0
  #2
Nov 2nd, 2007
What are your compiler errors?
Last edited by Belrog; Nov 2nd, 2007 at 3:53 am. Reason: Grammar mistakes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: returning three dimensional array

 
0
  #3
Nov 2nd, 2007
For example

  1. multi_return.c: In function ‘funct1’:
  2. multi_return.c:27: warning: return from incompatible pointer type
  3. multi_return.c:27: warning: function returns address of local variable

  1. #include <stdio.h>
  2.  
  3. float Mat[4][4][4];
  4.  
  5. float *funct1(void)
  6. {
  7. float Mat[4][4][4]={
  8. 1.2,1.3,1.4,1.5,
  9. 1.2,1.3,1.4,1.5,
  10. 1.2,1.3,1.4,1.5,
  11. 1.2,1.3,1.4,1.5,
  12.  
  13. 1.2,1.3,1.4,1.5,
  14. 1.2,1.3,1.4,1.5,
  15. 1.2,1.3,1.4,1.5,
  16. 1.2,1.3,1.4,1.5,
  17.  
  18. 1.2,1.3,1.4,1.5,
  19. 1.2,1.3,1.4,1.5,
  20. 1.2,1.3,1.4,1.5,
  21. 1.2,1.3,1.4,1.5,
  22.  
  23. 1.2,1.3,1.4,1.5,
  24. 1.2,1.3,1.4,1.5,
  25. 1.2,1.3,1.4,1.5,
  26. 1.2,1.3,1.4,1.5,};
  27. return Mat;
  28. }
  29.  
  30. void funct2()
  31. {
  32. int i,j,k;
  33. float *Mat;
  34. Mat=funct1();
  35.  
  36. for(k=0; k<4;k++)
  37. {
  38. for (i=0; i<4; i++)
  39. {
  40. for (j=0; j<4; j++)
  41. {
  42. // printf(" %f ", Mat[k][i][j]);
  43. }
  44. printf("\n");
  45. }
  46. printf("\n");
  47. }
  48.  
  49. }
  50.  
  51. int main() {
  52. funct2();
  53. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: returning three dimensional array

 
0
  #4
Nov 2nd, 2007
I'm sure you are getting a perfectly fine return value. Your problem is that you have not supplied any type information that the compiler can use to "know" about your array.

First, please see the Wikipedia here.

Next, please be aware that only the first dimension can be [] in C, and only when you are not actually creating data. Other dimensions must have a size. So
int Mat[][][];
is an error, as is
int Mat [][10][5];
but
void printarray( int a[][10][5], int depth );
is not.

However, int *Mat; knows nothing about how many dimensions the array has, or even if it really is an array.

If your array has variable size dimensions, then you should use a simple flat array (or pointer) and calculate things on your own:
  1. int *index_2d_array( int *arr, int height, int width, int y, int x ) {
  2. int index = (y * width) + x;
  3. return &(arr[ index ]);
  4. }
  5.  
  6. // two differently sized arrays with a "center" element
  7. int a[5][5] = {
  8. { 0, 0, 0, 0, 0 },
  9. { 0, 0, 0, 0, 0 },
  10. { 0, 0, 7, 0, 0 },
  11. { 0, 0, 0, 0, 0 },
  12. { 0, 0, 0, 0, 0 }
  13. };
  14. int b[3][5] = {
  15. { 0, 0, 0, 0, 0 },
  16. { 0, 0, 4, 0, 0 },
  17. { 0, 0, 0, 0, 0 }
  18. }
  19. // a[3][4] = b[1][3]
  20. *index_2d_array( a, 5, 5, 3, 4 ) = *index_2d_array( b, 3, 5, 1, 3 );

If your Mat object is a variable-sized array, you should create a small library of functions to handle it, and place all relevant information in a structure. For example:
  1. typedef struct {
  2. int *data;
  3. int width, height, depth;
  4. } Mat_t;
  5.  
  6. int *Mat_element( Mat_t Mat, int z, int y, int x );

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: returning three dimensional array

 
0
  #5
Nov 2nd, 2007
thanks

I think this one works fine

  1. #include <stdio.h>
  2.  
  3. float *array(void);
  4.  
  5. float *array(void)
  6. {
  7. static float Mat[4][4][4]={
  8. 1.2,1.3,1.4,1.5,
  9. 1.2,1.3,1.4,1.5,
  10. 1.2,1.3,1.4,1.5,
  11. 1.2,1.3,1.4,1.5,
  12.  
  13. 1.2,1.3,1.4,1.5,
  14. 1.2,1.3,1.4,1.5,
  15. 1.2,1.3,1.4,1.5,
  16. 1.2,1.3,1.4,1.5,
  17.  
  18. 1.2,1.3,1.4,1.5,
  19. 1.2,1.3,1.4,1.5,
  20. 1.2,1.3,1.4,1.5,
  21. 1.2,1.3,1.4,1.5,
  22.  
  23. 1.2,1.3,1.4,1.5,
  24. 1.2,1.3,1.4,1.5,
  25. 1.2,1.3,1.4,1.5,
  26. 1.2,1.3,1.4,1.5,};
  27.  
  28. return (float *) Mat;
  29. }
  30.  
  31.  
  32. int main(void)
  33. {
  34. float *Mat, i;
  35. Mat = array();
  36. for (i = 0; i < 16; i++) {
  37. printf("\n %f", *(Mat++));
  38. }
  39. return 0;
  40. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC