Archived

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

initializing an array

This topic is 5043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why can''t I initialize my array like this char array[pb->max] [16] ; where pb->max is an int inside a structure and pb is a pointer to that structure pb->max is 100 I get the error expected constant expression how can I solve this problem thanx in advance...

Share this post


Link to post
Share on other sites
you'd need to use pointers, I believe a non-pointer array like you're doing requires constant dimensions so that the compiler can sort it out properly.

Even then, with pointers, I'm not sure if you can do DMA on a 2D array where you only specify the first dimension. To be honest, the way I'd do things:


//define
char *array = new char[ pb->max * 16 ];

//use
array[ Y*pb->max + X ] = ...;

//delete
delete[] array;


hth
Jack


[edited by - jollyjeffers on February 22, 2004 6:46:15 AM]

Share this post


Link to post
Share on other sites
You can''t create an array just declaring it like you did if any of it''s dimensions is non-constant. Since the value of pb->max is only known in running time (and not in compile time) you must create the array dynamically, using array = new char[pb->max].

Blew

Share this post


Link to post
Share on other sites
Changed in what way? Being able to declare arrays with a variable for it''s size? I doubt it. Unless the declaration is converted to something else by the compiler...

Blew

Share this post


Link to post
Share on other sites
quote:
Original post by rypyr
Isn''t this changed in the latest C spec? (Note: C not C++)



Yes, C99 supports variable-length array at local scope.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites