Archived

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

initializing an array

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

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 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:

//definechar *array = new char[ pb->max * 16 ];//usearray[ Y*pb->max + X ] = ...;//deletedelete[] array;

hth
Jack

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

Share on other sites
I solved it a bit with a #define

but thanx anyway for your help

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 on other sites
Isn''t this changed in the latest C spec? (Note: C not C++)

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 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)