2

The following is a simplified version of what I'm trying to do, because I'm sure you don't want to wade through an entire set of structs and function prototypes for a particle system.

float const materials[24][4][4] = {{{...}}};
typedef struct EmitterStruct  { float *material[4][4]; } Emitter;
typedef struct ParticleStruct { float material[4][4]; } Particle;
Emitter *myEmitter;

Emitter * createEmitter(float *material[4][4])
{
    Emitter * newEmitter;
    newEmitter = (Emitter *)malloc(sizeof(Emitter));
    newEmitter->material = materal; /* Returns "incompatable types in assignment" */
    return newEmitter;              /* I also tried newEmitter->material = &material */
}

int main(char *argv, int argc)
{
    myEmitter = createEmitter(materials[0]);
}

In essence, as the comment shows, I get a compile error. I've tried this several different ways, even using "float material[4][4]" in the Emitter struct and the signature of createEmitter. However, then later on when I try to copy values into a particle for modificaitons using:

for (i=0; i++; i<4)
{
    for (j=0; j++; j<4)
    {
        particle->material[i][j] = emitter->material[i][j];
    }
}

I get another type mismatch when copying, even though everything is declared as type float[4][4]. In essence, I want to get a 4x4 array out of an array of 4x4 arrays, keep note of it in the emitter struct, then copy it into the particle struct. But I only want to actually copy the values one time.

4

4 回答 4

3

我正在回答您更新的问题(出现在您自己的答案中)。首先你的代码:

float const materials[24][4][4] = {{{...}}};
typedef struct EmitterStruct  { float *material; } Emitter; /*Use just a plain pointer*/
typedef struct ParticleStruct { float material[4][4]; } Particle;
Emitter *myEmitter;

Emitter * createEmitter(float *material) /*Use a plain pointer here*/
{
    Emitter * newEmitter;
    newEmitter = (Emitter *)malloc(sizeof(Emitter));
    newEmitter->material = material; 
    return newEmitter;               
}

int main(char *argv, int argc)
{
    myEmitter = createEmitter(materials[0]);/*This decays into a pointer*/
}

不!那不是正确的方法。它衰减为指针 - 是的,但不是指向浮点数的指针!如果您传递材料[0],您将得到一个float const[4][4]衰减到float const(*)[4](指向其第一个元素的指针),并且该指针就是传递的内容。因此,您想将其更改为:

float const materials[24][4][4] = {{{...}}};
/*Use just a plain pointer to an array */
typedef struct EmitterStruct  { float const (*material)[4]; } Emitter; 
typedef struct ParticleStruct { float material[4][4]; } Particle;
Emitter *myEmitter;

/*Use a plain pointer here. Bet keep it float const, not only float!*/
Emitter * createEmitter(float const (*material)[4])
{
    Emitter * newEmitter;
    newEmitter = (Emitter *)malloc(sizeof(Emitter));
    newEmitter->material = material; 
    return newEmitter;               
}

int main(int argc, char ** argv) /* you swapped args here */
{
    myEmitter = createEmitter(materials[0]); /* This decays into a pointer */
}

在这里阅读它:`int *userMask[3][4]` 指向什么?. 在此处阅读有关如何正确传递数组的信息:C++ 字符串:[] vs. *。我会推荐你​​一个好的 C 或 C++ 书友:)

于 2008-12-09T06:00:08.573 回答
1

In regard to the first snippet, you get that error because arrays in C are not assignable. You have to perform a memcpy to copy arrays.

In regard to the second snippet, you have an issue with the following line:

particle->material[i][j] = emitter->material[i][j];

The member material in Emitter is a 2d array of type float*. The member material in Particle is of type float. Note that one is a pointer and one is not, which is why they are not assignable.

You could write the following:

particle->material[i][j] = *(emitter->material[i][j]);

But that is assuming you've assigned those pointers to point at something. Alternatively, you can change material in Emitter to be a non-pointer. I can't tell you which you should do for sure because it is hard for me to decipher what your exact intentions are based on the code you've given.

于 2008-12-09T05:05:57.277 回答
0

Of course, the second I ask for help, I figure out exactly what I needed to do.

float const materials[24][4][4] = {{{...}}};
typedef struct EmitterStruct  { float *material; } Emitter; /*Use just a plain pointer*/
typedef struct ParticleStruct { float material[4][4]; } Particle;
Emitter *myEmitter;

Emitter * createEmitter(float *material) /*Use a plain pointer here*/
{
    Emitter * newEmitter;
    newEmitter = (Emitter *)malloc(sizeof(Emitter));
    newEmitter->material = material; 
    return newEmitter;               
}

int main(char *argv, int argc)
{
    myEmitter = createEmitter(materials[0]);/*This decays into a pointer*/
}

And then for the copy:

for (i=0; i++; i<4)
{
    for (j=0; j++; j<4)
    {
        particle->material[i][j] = *(emitter->material[i * 4 + j];
    }
}

Doh...

于 2008-12-09T05:08:44.153 回答
0

我曾经是这方面的专家。不幸的是,我已经离开它太久了。所以这是我大约 6 年前写的一个工作代码片段。我认为它可能会为您指明正确的方向。

// get a color histogram of an image
int ***colorHistogram(PIXEL *inputImage, HEADER *imageHeader)
{
    int x, y, z;

    // a color histogram
    int ***histo;

    // allocate space for the histogram
    histo = (int ***)malloc(256 * sizeof(int**));
    for(x=0; x<256; x++)
    {
        histo[x]=(int **)malloc(256 * sizeof(int*));
        for(y=0; y<256; y++)
        {
            histo[x][y]=(int *)malloc(256 * sizeof(int));

            // initialize the histogram
            for(z=0; z<256; z++)
                histo[x][y][z] = 0;
        }
    }

    // fill the histogram
    for (x = 0; x < imageHeader->width * imageHeader->height; x++)
    {
        histo[((int) inputImage[x].r)][((int) inputImage[x].g)][((int) inputImage[x].b)]++;
    }

return histo;

}

无论如何,我希望这会有所帮助。

于 2008-12-09T05:12:37.420 回答