How to return a multidimensional array with a pointer function?

Hello,

I would like to return a multidimensional array from a pointer function.But multidimensional array dimension's will be enormous.For example,width will be 500 and height will be 600.

Could you help me please?
1
2
3
4
5
6
7
8
9
10
11
12
char** returnDoublePointer(int i)
{
	char **pptr;

	pptr = new char*[i];
	for(int j = 0;j < i;j++)
	{
		*(pptr + i) = new char[i];
	}
	
	return pptr;
}
If you are using C++, stick to using classes like vector or deque.

If you are using C, it is worth your time to create a struct containing useful members:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
typedef struct multarray_int_tag
  {
  unsigned rows;
  unsigned columns;
  int**    data;
  }
  multarray_int_t;

multarray_int_t createIntMultArray( unsigned rows, unsigned columns )
  {
  unsigned        cntr;
  multarray_int_t result;

  result.rows    = rows;
  result.columns = columns;
  result.data    = (int**)malloc( sizeof( int* ) * rows );
  for (cntr = 0; cntr < columns; cntr++)
    result.data[ cntr ] = (int*)malloc( sizeof( int ) * columns );

  return result;
  };

multarray_int_t freeIntMultArray( multarray_int_t a )
  {
  unsigned cntr;

  for (cntr = 0; cntr < a.rows; cntr++)
    free( (void*)a.data[ cntr ] );
  free( (void*)a.data );

  a.rows    =
  a.columns = 0;
  a.data    = NULL;

  return a;
  };

Now you can just pass around the structure as your 2D array when you want to use it in your program.

Hope this helps.
Topic archived. No new replies allowed.