tags:

views:

87

answers:

2

I have four arrays of data (3072 bytes each.) I want to point to one of these arrays, so if a function were passed a pointer to these arrays, it could manipulate them, and the pointers could be s

So initially I thought of pointers-to-pointers:

uint16_t *buffer0_level[BUFF_WORDS] FAR;
uint16_t *buffer0_mask[BUFF_WORDS] FAR;
uint16_t *buffer1_level[BUFF_WORDS] FAR;
uint16_t *buffer1_mask[BUFF_WORDS] FAR;

uint16_t **draw_buffer_level;
uint16_t **draw_buffer_mask;
uint16_t **disp_buffer_level;
uint16_t **disp_buffer_mask;

And then to set the pointers I'd do something like this:

draw_buffer_level = buffer0_level;
draw_buffer_mask = buffer0_mask;
disp_buffer_level = buffer1_level;
disp_buffer_mask = buffer1_mask;

This seems to work.

However, I then want to pass the pointer to a function, fill_buffer, which fills one of the arrays with some data word.

The definition is:

void fill_buffer(uint16_t *buff, uint16_t word)

But when calling it like this:

fill_buffer(draw_buffer_level, 0x0000);
fill_buffer(draw_buffer_mask, 0x0000);
fill_buffer(disp_buffer_level, 0x0000);
fill_buffer(disp_buffer_mask, 0x0000);

GCC complains:

gfx.c:46: warning: passing argument 1 of 'fill_buffer' from incompatible pointer type
gfx.c:47: warning: passing argument 1 of 'fill_buffer' from incompatible pointer type
gfx.c:48: warning: passing argument 1 of 'fill_buffer' from incompatible pointer type
gfx.c:49: warning: passing argument 1 of 'fill_buffer' from incompatible pointer type

I'm sure it's just a stupid error, but what is it? I'm still a novice to C programming.

A: 

you need to dereference the pointer-to-pointer.

fill_buffer(*draw_buffer_level, 0x0000);
San Jacinto
+2  A: 

I am assuming you want to allocate BUFF_WORDS long buffers of 16 bit words, not pointers. For this you would need to change your declarations like so,

uint16_t buffer0_level[BUFF_WORDS] FAR;
uint16_t buffer0_mask[BUFF_WORDS] FAR;
uint16_t buffer1_level[BUFF_WORDS] FAR;
uint16_t buffer1_mask[BUFF_WORDS] FAR;

You don't really need to declare pointers to have your function initialize the arrays. You can pass the array names (which are pointers anyways) directly to the function. But you do have to pass the array length into the function. Like so,

fill_buffer(buffer0_level, BUFF_WORDS, 0x0000);
fill_buffer(buffer0_mask, BUFF_WORDS, 0x0000);
fill_buffer(buffer1_level, BUFF_WORDS, 0x0000);
fill_buffer(buffer1_mask, BUFF_WORDS, 0x0000);

Then you would implement your function like so,

void fill_buffer(uint16_t* buffer, int count, uint16_t value)
{
    int ii;
    for (ii = 0; ii < count; ++ii)
    {
        *buffer++ = value;
    }
}
Ziffusion
Note that array names are *not* pointers, but when used in most contexts they evaluate to pointer values (passing them to another function is one such context).
caf