开发者

Pointer to fixed size array indexing

Consider the following code:

typedef float image_buffer[1024][1024];

void f(image_buffer *b)
{
    for (int i = 0; i < 1024; i++)
    {
        for (int j = 0; j < 1024; j++)
        {
          开发者_开发问答  b[i][j] = 0; // doesn't work
            (*b)[i][j] = 0; // also doesn't work
        }
    }
}

People are complaining that there is no question, but the question is obvious, how do you index fixed size array having a pointer to it...? Thank you!

EDIT: OpenCL code:

typedef float image_buffer[1024][1024];
__kernel void kernel1(sampler_t smp, read_only image2d_t a, read_only image2d_t b, __global image_buffer *r)
{
    __local float shared[16][16];

    float4 colorA = read_imagef(a, smp, (int2)(get_global_id(0), get_global_id(1))),
           colorB = read_imagef(b, smp, (int2)(get_global_id(0), get_global_id(1)));

    (*r)[get_global_id(0)][get_global_id(1)] = (colorA.x - colorB.x) * (colorA.x - colorB.x) + (colorA.y - colorB.y) * (colorA.y - colorB.y) + (colorA.z - colorB.z) * (colorA.z - colorB.z) + (colorA.w - colorB.w) * (colorA.w - colorB.w);
}


Perhaps you meant to write:

typedef float** image_buffer;

In any case, the correct syntax would be:

(*b)[i][j] = 0;


Consider:

typedef float image_buffer[1024][1024];

void f(image_buffer *b)
    {
    for (int i = 0; i < 1024; i++)
        {
        for (int j = 0; j < 1024; j++)
            {
            (*b)[i][j] = 0; // this works on gcc -std=c99 $FILE
            }
        }
    }

No main, so linker error, but it compiles fine.


Well, you are making an invalid assertion that something "doesn't work", while in reality it works perfectly fine. Which is why people might complain that there's no actual question in your "question".

The proper way to access you array inside the function is

void f(image_buffer *b)
{
  ...
  (*b)[i][j] = 0;
  ...
}

And yes, it works. If it "doesn't work" for you, you have to explain what you mean exactly. "Doesn't work" is not really a meaningful explanation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜