Variable Length Arrays

Posted By: boatman

Variable Length Arrays - 11/21/14 02:36

Hello

I seem to be unable to create an array that uses a variable for its length. This returns a syntax error. For example:

var HighCutoff = 48;
var my_arry[HighCutoff+1];

I also tried declaring the length variable as an int, not a var. Same result.

From my googling, this seems to be a limitation of Lite-C. Is that correct? Does anyone know of a workaround?

Thanks
Posted By: kandlekid

Re: Variable Length Arrays - 11/21/14 05:16

Haven't tried it yet, but maybe using the pre-processor ...

#define HighCutoff 48

var my_arry[HighCutoff+1];
Posted By: DdlV

Re: Variable Length Arrays - 11/21/14 08:06

Hi boatman, kandlekid. Depends on what you're trying to accomplish. kandlekid's #define parameterizes the code, but the array's size is still defined a compile time. If what you're after is an array dynamically defined at run time, then I believe you need to be using malloc, etc.

HTH.
Posted By: boatman

Re: Variable Length Arrays - 11/24/14 22:20

Hi DdlV and kandlekid. Thanks for your suggestions, much appreciated. I can't make it work using the pre-processor; this seems to result in the same syntax error. I'll give malloc a try - don't know anything about this function yet, so I will have a look at the manual and do some googling.

Thanks again
© 2024 lite-C Forums