When you pass an array to a function, you don't actually pass the array by value. Rather, you actually pass a reference to the original array.
Therefore, you simply need to do:
void ftn(unsigned short buffer[], int size)
{
for (int i = 0; i < size; i++)
{
buffer[i] = 0; //test
}
}
Note that if you change the actual value of buffer
, you don't actually change the original array. An example of this could be:
void ftn(unsigned short buffer[], int size)
{
buffer = new unsigned short[20];
}
If you wish to change the original array, your construct will work, but with a little modification:
void ftn(unsigned short **buffer, int size)
{
for (int i = 0; i < size; i++)
{
(*buffer)[i] = 0; //test
}
}
This is very C-like, mind you, and less C++-like.
buffer
is a pointer to a pointer to an unsigned short
. (That is, a pointer to the variable you use to refer to the array.)
If you dereference buffer
, you get a pointer to an unsigned short
, which can be treated as an array of unsigned short
s.
With this you also have the ability to reassign the value of the original variable, for instance like this:
void ftn(unsigned short **buffer, int size)
{
*buffer = new unsigned short[20];
}
See also: