June 16, 2024, 07:57:42 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Silly question time again - setting array sizes

Started by Andy, October 14, 2015, 02:43:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

I was wondering why I cannot use an INT to set an array size.

If I use:
STRING MyString[1000] - that's okay - no problem.

But what if I don't want to use up memory, and I don't yet know how big to make the array?

E.g. If I can count up something, and know the size I need is say 876, why do I get a compile error when I try this....


1. INT CountUp = (say 876)
2. STRING MyString[CountUP]

I get "duplicate definition of label or variable: CountUp" error.

Was just wondering about this, and can it be done?

Thanks,
Andy.




Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Brian

Andy,

There is a workaround for this - I remember Paul Turley telling me once. I would have to look
at my myriad bits of code at home to find it, though. Some Basics have a REDIM command,
where you set an initial value, do a count of your total records, and then REDIM your initial
command. Would be nice in IWB, I reckon

Brian

LarryMc

when you declare variables, regardless of where they are located in your program they are processed during the compile time and not during run time.
Taking your two lines of code I'll describe what happened during compile time:
INT CountUp = 876
allocates memory for a variable named CountUp and would normally initialize it to 0 or not based on a flag in Compiler Options but in this case because of "=876" it is initialized to 876. Since the program has yet to me compiled the value is not used for anything at this point.

STRING MyString[CountUP]
the compiler recognizes this as a string array so it looks in the brackets for an INT value for the number of elements in the array.  The compiler, when it finds a variable where a INT value is expected, attempts to DEFINE the variable (just like INT CountUp) and that is where the duplicate variable error comes from.
FYI - this automatic defining of variable types by the compiler can be controlled by the user with the AUTODEFINE  on/off command but not in this case

Now, this will work
CONST CountUp =  876
STRING MyString[CountUP]

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bruce Peaslee

I do not believe arrays can be re-dimensioned. If I need to store an indeterminate amount of data, I use linked lists.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

ckoehn

This is a post from Paul..

Quotepointer p
p = new(INT, size)
#p[index] = value
print #p[index]
delete p

To change the size of the array you have two choices.  Use GlobalRealloc from the Windows API or allocate a second array the new size and copy the old contents.

Later,
Clint

Andy

Thanks everyone for your posts, I will have a play around with the suggestions.

Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

I'm 3/4 of the way  through a long tutorial/example that I'll try to get posted later today.
Don't give up on me.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library