array initialization

New to FreeBASIC? Post your questions here.
Post Reply
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

array initialization

Post by PeterHu »

I could not understand why in sub s1() the array is not availbe yet as code shown below.

And also how to declare a const array in module level as

Code: Select all

const fruitname(0 to 2) as string=>{...}
Help would be appreciated.

Code: Select all

dim fruitname(0 to 2) as string => {"apple", "orange", "banana"}

? fruitname(0)  '''OK,pass
sleep

sub s1()
    ? fruitname(0) '''No,failed:Variable not declared,furitname in 'fruitname(0)'
end sub

SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: array initialization

Post by SARG »

- Not sure you can use const with array.

- Use the array as parameter :

Code: Select all

dim fruitname(0 to 2) as string => {"apple", "orange", "banana"}

sub s1(fruitname2() as string)
    ? fruitname2(0) '''No,failed:Variable not declared,furitname in 'fruitname(0)'
    ? fruitname2(1) '''No,failed:Variable not declared,furitname in 'fruitname(0)'
end sub

? fruitname(0)  '''OK,pass
?"========"
s1(fruitname())

sleep
It's also possible to define 'shared' the array so no need to pass as parameter but you can't initialize it with dim.
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

Re: array initialization

Post by PeterHu »

Thank you. Below code works now.

Code: Select all

'''Error: Var-len strings cannot be initialized in 
''''dim shared fruitname(0 to 2) as string  => {"apple", "orange", "banana"}'

dim shared fruitname(0 to 2) as string * 20 => {"apple", "orange", "banana"}

? fruitname(0)  

sub s1()
? fruitname(0) 
end sub

s1()

sub main()
	s1()
	sleep
end sub

main

SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: array initialization

Post by SARG »

Good find. I didn'tt think about string * N ;-)
fxm
Moderator
Posts: 12133
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: array initialization

Post by fxm »

Since fbc version 1.20.0, the compiler always pads the STRING*N type with spaces.
If you do not want spaces on the right side of your string, use rather the ZSTRING*N type:

Code: Select all

dim shared fruitname(0 to 2) as zstring * 20 => {"apple", "orange", "banana"}
(with this type ZSTRING*20, only a maximum of 20-1 characters are available to the user for each array element)
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: array initialization

Post by Lost Zergling »

About :
'''Error: Var-len strings cannot be initialized in
''''dim shared fruitname(0 to 2) as string => {"apple", "orange", "banana"}'

Using another approach ;-)
viewtopic.php?t=26551 (lzle doc/demo)
viewtopic.php?f=8&t=26533 (lzle lib)
viewtopic.php?f=8&t=27695 (lzae lib)

Lzae exemple 5, replace :
Dim aext As ArrayExtension
by
Dim Shared aext As ArrayExtension

A little bit heavy, just a little bit :P :lol:
Compile fine, handle it shared on requirement no matter var len, troughout an aext array extension
ps : may not be compatible with any lzle releases ! :mrgreen:
fxm
Moderator
Posts: 12133
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: array initialization

Post by fxm »

Without going that far, you can more simply encapsulate the declaration+initialization of your var-len array in a Type.
This will cause the compiler to add a constructor, and an instance of this type can then be declared shared:

Code: Select all

type udt
    dim fruitname(0 to 2) as string => {"apple", "orange", "banana"}
End type
dim shared u as udt

sub s1()
    ? u.fruitname(1)
    ? u.fruitname(2)
end sub

? u.fruitname(0)
s1()

sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: array initialization

Post by dodicat »

A little macro to fill an array without too much typing:

Code: Select all



#macro fill(a,r,datatype,d...)
Redim a(r)
Scope
    Dim As datatype x(r)=d
    For n As Long=0 To Ubound(a)
        a(n)=x(n)
    Next
End Scope      
#endmacro

Dim Shared As String f()
fill(f,2,String,{"apple", "orange", "banana"})


Dim Shared As Double d()
fill(d,4,Double,{Rnd,Rnd,Rnd,Rnd,Rnd})

sub dothis
    For n As Long=0 To Ubound(f)
    Print n,f(n)
Next
Print
For n As Long=0 To Ubound(d)
    Print n,d(n)
Next
print
end sub

dothis

Sleep



 
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

Re: array initialization

Post by PeterHu »

I learnt from this post much more than I expected.

Thank you all.
Post Reply