Archived

This topic is now archived and is closed to further replies.

RomanR

Array Initialization

Recommended Posts

I have a bit of a problem initializing a 2d array with color data from an image.
Here is what I have: UINT w,h which are initialized with width and height of the image like so:
w = img->GetWidth();
h = img->GetHeight();
 
Now initializing an array like so:
D3DCOLOR c[w][h];
 
does not compile.
C:\Projects\Engine\Engine.cpp(42) : error C2057: expected constant expression
C:\Projects\Engine\Engine.cpp(42) : error C2466: cannot allocate an array of constant size 0
I know that w and h get initialised to the right width and height. Please assist!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Its not possible to statically declare an array using variable sizes. IF your code looked like

D3DCOLOR c[10][10];

...that would work.

What you need in your case is dynamic allocation.

D3DCOLOR *c = new D3DCOLOR[w*h]; should work fine

Oh and remember to do a

delete [] c;

when you dont need the array anymore.

Hope that helps

-MrZ

Share this post


Link to post
Share on other sites
Ok, wouldn''t this declare a 1d array? I tried this with just 1 index like so: *c = pixelColor; I get "illegal indirection" error. *c[i][j] does not let me compile either. Please explain your way in a bit more depth please?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you can''t declare the array inline with variables the way you are trying, only with constants. To work with variables, you need to create the array dynamically:

D3DCOLOR *c = new D3DCOLOR[w];
for (int i=0; i<w; i++)
c = new D3DCOLOR[h];

and when you are done, you need to delete the memory in the same way:

for (int j=0; j<w; j++)
delete c[j];
delete [] c;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
<quote>
D3DCOLOR *c = new D3DCOLOR[w];
for (int i=0; i<w; i++)
c = new D3DCOLOR[h];
</quote>

of course that should''ve read:

D3DCOLOR *c = new D3DCOLOR[w];
for (int i=0; i<w; i++)
c<b></b> = new D3DCOLOR[h];

This board really does need a preview button for posters...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
let''s try that again... it should''ve read

D3DCOLOR *c = new D3DCOLOR[w];
for (int i=0; i<w; i++)
c = new D3DCOLOR[h];

notice that i''m assigning a new value to c[i], not overwriting c.

I reiterate on the preview button... i''m gonna go register now, so i can edit my next post

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
D3DCOLOR *c = new D3DCOLOR[w];
for (int i=0; i<w; i++)
c = new D3DCOLOR[h];


if this doesn''t work, I give up...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i figured out the problem, it''s because it interprets blocks for html, and its turning my index into an italisize(sp) tag. With modified for flags, it looks like this:

D3DCOLOR *c = new D3DCOLOR[w];
for (int x=0; x<w; x++)
{
c[x] = new D3DCOLOR[h];
}


for (int y=0; y<w; y++)
{
delete c[y];
}
delete [] c;

Share this post


Link to post
Share on other sites
Heh. I noticed. And I'm registered now... maybe now I can go back and edit stuff instead of doing multiple corrective posts. *sigh*

What board does this site run? Is there a list of the commands and other stuff it seems to use.

(like using blocks for html instead <>)

Give a man fire and he''ll be warm for the night. Set a man on fire and he''ll be warm for the rest of his life.

--

Edited by - KwikSilvr on December 18, 2001 10:43:06 AM

Share this post


Link to post
Share on other sites
quote:
Original post by KwikSilvr
What board does this site run? Is there a list of the commands and other stuff it seems to use.(like using blocks for html instead <>)

i think they wrote their own for gamedev.net (i remember reading that you can license it or something if you want)... there is a link to the forum FAQ at the top of this page, that tells a bit about formatting and stuff...

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
quote:

Ok, wouldn't this declare a 1d array?



Yes it would, but you can still use it as if it was 2 dimensional.
[pre]
C[3][5] would be the same as c[(5*w)+3];
[/pre]

Definately - a preview button...

Edited by - origil on December 18, 2001 11:02:35 AM

Share this post


Link to post
Share on other sites
Anon poster I think your code will fail no? you are declaring c to by a 1d array and not a 2d array

You are doing the following...

  
// Should this not be changed to... D3DColor **c = new *D3DColor[w]

D3DColor *c = new D3DColor[w];

for(int x = 0; x < w; x++)
{
// And this change to *c[x] = new D3DColor[h];

c[x] = new D3DColor[h];
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ANSI2000
Anon poster I think your code will fail no? you are declaring c to by a 1d array and not a 2d array

You are doing the following...


    
// Should this not be changed to... D3DColor **c = new *D3DColor[w]

D3DColor *c = new D3DColor[w];

for(int x = 0; x < w; x++)
{
// And this change to *c[x] = new D3DColor[h];

c[x] = new D3DColor[h];
}



you are quite correct. Just goes to show, don''t respond in the morning before you have coffee. And if you make a mistake, don''t propogate it through use of cut & paste.

Share this post


Link to post
Share on other sites