1

I have a buffer of points that I want to send off to be processed by a function. It is an array of unsigned shorts. Currently I'm trying the following:

void ftn(unsigned short **buffer, int size)
{
    for (int i = 0; i < size; i++)
    {
        *(buffer[i]) = 0; //test
    }
}

Outside of the function, it's defined as unsigned short buffer[size]; Does this not make sense? Where am I going wrong? Thanks

apple
  • 63
  • 1
  • 4

3 Answers3

3

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 shorts.

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:

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • I think that this answer is still too abstract. What is actually happening is that a pointer is being passed by value. Consider `void ftn(unsigned short buffer[], int size) { buffer = new unsigned short[size * 2]; }` . Nothing is happening to the original array, the pointer parameter is just being reassigned; `buffer` isn't a reference to an array. – CB Bailey Feb 06 '11 at 16:29
  • P: With respect to your edit, the call to the function would just pass the buffer and its size? Trying to use your example of (*buffer)[i] leads to bad pointer references – apple Feb 06 '11 at 16:40
  • @apple: Unless you need to allocate a new array instead of the old one, stick with the first version I gave. Remember, you can still change the values within the array in that version, it's only the array itself you cannot change. – Sebastian Paaske Tørholm Feb 06 '11 at 17:47
0

No that doesn't make sense, because buffer[i] "returns" an array of shorts, and the *(buffer[i]) tries to dereference that array of shorts, and then, you try to set a memory address for that (i.e. NULL or 0), and that results in a seg fault.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
  • Thanks for explaining, but just for clarification, how does *(buffer[i]) try to dereference an array of shorts. Shouldn't it be trying to dereference a single short in an array? This is where my confusion stems from. – apple Feb 06 '11 at 16:28
  • 1
    `*(buffer[i])` evaluates `buffer[i]` first, and then applies `*` to the result. `buffer[i]` means "treat `buffer` as an array and grab the `i`th element". You shouldn't be trying to dereference a short, anyway, because you dereference pointers, and shorts are not pointers. – Karl Knechtel Feb 06 '11 at 17:16
0

The answer is very quick, I don't know why people wrote so much:

#define SIZE_ARRAY 10

void ftn(unsigned short * buffer, int size)
{
    for (int i = 0; i < size; i++)
    {
        buffer[i] = 0; // test
    }
}

void main()
{
    unsigned short buffer[SIZE_ARRAY];
    ftn(&buffer[0], SIZE_ARRAY);
}

I wouldn't take Sebastian's answer he still uses unsigned short **buffer which is just too complication.

Poni
  • 11,061
  • 25
  • 80
  • 121