Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:56 17 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Strings defined with length n?

Author Message
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 05:37pm 10 Feb 2016
Copy link to clipboard 
Print this post

Hi all,

Can someone tell me why defining a string with more than n characters does not generate an error (page 55 of the MM manual indicates it should.).

See my example below :-

Using an MM with 5.1 loaded.

> dim t$(3) as string length 3
> t$(0) = "abc"
> t$(1) = "def"
> t$(2) = "ghi"
> t$(3) = "jkl"
> for i = 0 to 3 : ? t$(i) : next
abc
def
ghi
jkl
> t$(2) = "mnopq"
> for i = 0 to 3 : ? t$(i) : next
abc
def
mnopq
jkl
> t$(4) = "xyz"
Error: Array index out of bounds
>


As you can see referencing an out of bounds element does cause an error (as expected).
Rob White
 
plasma
Guru

Joined: 08/04/2012
Location: Germany
Posts: 437
Posted: 08:46pm 10 Feb 2016
Copy link to clipboard 
Print this post

T$(4) is the error
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 09:00pm 10 Feb 2016
Copy link to clipboard 
Print this post

Yes that is correct. I put it there to demonstrate that error messages were being generated.

My question is why doesn't setting T$(2) = "mnopq" generate an error that the length of the string is out of bounds? Interestingly no data is lost (overwritten - T$(3) still has it's correct value)
Rob White
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 10:34pm 10 Feb 2016
Copy link to clipboard 
Print this post

one for Geoff I think!
 
kiiid

Guru

Joined: 11/05/2013
Location: United Kingdom
Posts: 671
Posted: 11:31pm 10 Feb 2016
Copy link to clipboard 
Print this post

You are trying to define arrays but not fixed length strings
http://rittle.org

--------------
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 12:05am 11 Feb 2016
Copy link to clipboard 
Print this post

Thanks Kiiid,

Looks like the order in the dim statement is critical. The last part of my definition is being ignored.

This works fine :- dim as string t$(3) length 3
Rob White
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 03:40am 11 Feb 2016
Copy link to clipboard 
Print this post

I got confused reading the manual about using length and arrays.
And i already program in basic for about 35 years.

Microblocks. Build with logic.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 04:09am 11 Feb 2016
Copy link to clipboard 
Print this post

. . . . took me three times to read it before I spotted the 'difference'.

Bizzie's reference to:

DIM AS STRING t$(3) LENGTH 3

is a useful future reference (for me at least!).
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025