Dear gurus,
This is about returning an array from a function.
First, in the function, an array is created by allocating space from the
heap (line 14 and 18 below).
Then its pointer is returned (line 23).
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 37 38 39 40

#include <iostream>
#include <stdint.h>
#define LEVEL 10
using namespace std;
uint16_t ** get_RGBfreq()
{
uint16_t ** tempArray;
tempArray = new uint16_t * [3];
for (int i=0; i < 3; i++)
{
tempArray[i] = new uint16_t [ LEVEL + 1];
for (int j=0; j <= LEVEL; j++)
tempArray[i][j] = 777;
}
return tempArray;
}
int main()
{
uint16_t ** myRGBfreq = get_RGBfreq();
for (int i=0; i < 3; i++)
{
for (int j=0; j <= LEVEL; j++)
cout << "myRGBfreq[" << i << "][" << j << "] is " << myRGBfreq[i][j] << endl;
cout << endl;
}
return 0;
}

This program works fine.
Its output is below:
myRGBfreq[0][0] is 777
myRGBfreq[0][1] is 777
myRGBfreq[0][2] is 777
myRGBfreq[0][3] is 777
myRGBfreq[0][4] is 777
myRGBfreq[0][5] is 777
myRGBfreq[0][6] is 777
myRGBfreq[0][7] is 777
myRGBfreq[0][8] is 777
myRGBfreq[0][9] is 777
myRGBfreq[0][10] is 777
myRGBfreq[1][0] is 777
myRGBfreq[1][1] is 777
myRGBfreq[1][2] is 777
myRGBfreq[1][3] is 777
myRGBfreq[1][4] is 777
myRGBfreq[1][5] is 777
myRGBfreq[1][6] is 777
myRGBfreq[1][7] is 777
myRGBfreq[1][8] is 777
myRGBfreq[1][9] is 777
myRGBfreq[1][10] is 777
myRGBfreq[2][0] is 777
myRGBfreq[2][1] is 777
myRGBfreq[2][2] is 777
myRGBfreq[2][3] is 777
myRGBfreq[2][4] is 777
myRGBfreq[2][5] is 777
myRGBfreq[2][6] is 777
myRGBfreq[2][7] is 777
myRGBfreq[2][8] is 777
myRGBfreq[2][9] is 777
myRGBfreq[2][10] is 777

In this case, the function
get_RGBfreq()
returns an array, as expected.
Question
If I want to create the array in the stack (not the heap) instead, by using
unint16_t tempArray[3][LEVEL]
(i) Is it possible to return this array from the function?
(ii) If yes, how to write the code?
(iii) Which (using
heap or
stack) is the better way of doing this, particularly in my case as above?
Note:
I heard we should avoid pointer if there is alternative.
That's why I attempt to use the stack instead of heap.
Thanks in advance.