User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 373,495 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,887 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:

returning three dimensional array

Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,733
Reputation: Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all 
Rep Power: 9
Solved Threads: 172
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: returning three dimensional array

  #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:
int *index_2d_array( int *arr, int height, int width, int y, int x ) {
  int index = (y * width) + x;
  return &(arr[ index ]);
  }

// two differently sized arrays with a "center" element
int a[5][5] = {
  { 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0 },
  { 0, 0, 7, 0, 0 },
  { 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0 }
  };
int b[3][5] = {
  { 0, 0, 0, 0, 0 },
  { 0, 0, 4, 0, 0 },
  { 0, 0, 0, 0, 0 }
  }
// a[3][4] = b[1][3]
*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:
typedef struct {
  int *data;
  int width, height, depth;
  } Mat_t;

int *Mat_element( Mat_t Mat, int z, int y, int x );

Hope this helps.
Reply With Quote  
All times are GMT -4. The time now is 6:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC