tokyotech

Distinguished
Mar 18, 2008
10
0
18,560
I have two questions about arrays.

1) I created an array with [][] and passed it into a function expecting **. I got an error saying: cannot convert parameter 1 from 'gfx::Vec3f [800][600]' to 'gfx::Vec3f **. I thought [][] and ** are the same thing!

2) I tried doing "Vec3f image[width][height];" before, but the compiler complained saying "expected constant expression". So I changed it to "Vec3f image[800][600];" to make it compile. Why must I do this hardcoding?

[cpp]
Pixels::pixels(Vec3f** colors, int width, int height)
{
this->width = width;
this->height = height;
this->image = colors;
}
[/cpp]

[cpp]
int width = 800;
int height = 600;

Vec3f image[800][600];

for(int w = 0; w < width; w++)
for(int h = 0; h < height; h++)
image[w][h] = Vec3f(w / width, h / height, w / height);

Pixels pixels = new Pixels(image, width, height);
[/cpp]
 
G

Guest

Guest
In C++ you have to use literal values for array sizes. To solve this you will have to use pointers like so:
[cpp]Vec3f ** image;

// Then the C way
image = (Vec3f **)malloc(sizeof(Vec3f *)*width)
for(int i = 0; i < width; ++i)
image = (Vec3f *)malloc(sizeof(Vec3f)*height);

// or the C++ way
image = new Vec3f*[width];
for(int i = 0; i < width; ++i)
image = new Vec3f[height];

// And then use them just as before with image[w][h][/cpp]