array bounds: bug or feature?

Just starting out? Need help? Post your questions and find answers here.
Tiuri
User
User
Posts: 20
Joined: Fri Aug 15, 2003 2:44 am
Location: Canberra, Australia

array bounds: bug or feature?

Post by Tiuri »

I noticed something strange with the definition of array bounds

Code: Select all

OpenConsole()
;
Dim a.l(10)    

Structure array
a.l[10]
EndStructure

DefType.array b
n=10
For i=0 To n : a(i)=i :Next  
For i=0 To n : b\a[i]=i :Next  

n.l=Val(Input())
CloseConsole()
End
In the above code you can use elements 0-10 of the array defined in the DIM statement (that is, 11 elements) without the Debugger protesting about out of bounds. For the array declared in the structure however it works as I would expect, i.e. b\a[10] gives an out of bounds. Is this really as intended? Do I really get an 11 element array when I declare
Dim a.l(10), or is it the Debugger?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Yes, in structure an array [] doesn't behave like the normal BASIC array behaviour to conform to C/C++ structure format (to allow direct API structure porting). This means than a[2] will allocate an array from 0 to 1 where Dim a(2) will allocate an array from 0 to 2. It should be added to the docs, I agree.
Tiuri
User
User
Posts: 20
Joined: Fri Aug 15, 2003 2:44 am
Location: Canberra, Australia

Post by Tiuri »

Thanks for clarifying that. On the basis of the documentation I had expected that Dim a.l(10) would give you 10 elements, especially given the example of a 2-dim array with the number of columns and lines. I am not familiar with the standard BASIC definitions, do BASIC arrays also start with index 0 instead of 1? If they start with 1 (a much more reasonable choice in my view, but I lose against the enormous influence of C\C++ ) it would all make more sense....
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> do BASIC arrays also start with index 0 instead of 1?

In every Basic I've used (from the Vic-20 up) arrays always start at index
number 0. So Dim MyArray(5) is actually 6 arrays. I think Visual Basic
lets you start at 1 instead, but since getting PureBasic I haven't touched
VB and can't be 100% sure of that anymore. :wink:
Post Reply