944,038 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8842
  • C RSS
Nov 2nd, 2007
0

returning three dimensional array

Expand Post »
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 ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phylon is offline Offline
6 posts
since Oct 2007
Nov 2nd, 2007
0

Re: returning three dimensional array

What are your compiler errors?
Last edited by Belrog; Nov 2nd, 2007 at 3:53 am. Reason: Grammar mistakes
Reputation Points: 11
Solved Threads: 4
Junior Poster in Training
Belrog is offline Offline
71 posts
since Nov 2007
Nov 2nd, 2007
0

Re: returning three dimensional array

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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
mank is offline Offline
41 posts
since Oct 2007
Nov 2nd, 2007
0

Re: returning three dimensional array

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 2nd, 2007
0

Re: returning three dimensional array

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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
mank is offline Offline
41 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help in a very easy C progrem
Next Thread in C Forum Timeline: Copy argv to string in C(newbie question)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC