Howto initialise string array

General FreeBASIC programming questions.
RockTheSchock
Posts: 220
Joined: Mar 12, 2006 16:25

Howto initialise string array

Postby RockTheSchock » Aug 15, 2016 23:04

I would like to Initialise an array but i get:

FreeBasic/src/compiler/enumstrings.bi(46) error 86: Var-len strings cannot be initialized in '"AST_CLASSES" }'

Code: Select all

Dim Shared As String AST_NODECLASS_STR(0 TO ...) = {_
"AST_NODECLASS_NOP",_
"AST_NODECLASS_LOAD",_
"AST_NODECLASS_ASSIGN",_
"AST_NODECLASS_BOP",_
"AST_NODECLASS_UOP",_
"AST_NODECLASS_CONV",_
"AST_NODECLASS_ADDROF",_
"AST_NODECLASS_BRANCH",_
"AST_NODECLASS_JMPTB",_
"AST_NODECLASS_CALL",_
"AST_NODECLASS_CALLCTOR",_
"AST_NODECLASS_STACK",_
"AST_NODECLASS_MEM",_
"AST_NODECLASS_LOOP",_
"AST_NODECLASS_COMP",_
"AST_NODECLASS_LINK",_
"AST_NODECLASS_CONST",_
"AST_NODECLASS_VAR",_
"AST_NODECLASS_IDX",_
"AST_NODECLASS_FIELD",_
"AST_NODECLASS_DEREF",_
"AST_NODECLASS_LABEL",_
"AST_NODECLASS_ARG",_
"AST_NODECLASS_OFFSET",_
"AST_NODECLASS_DECL",_
"AST_NODECLASS_NIDXARRAY",_
"AST_NODECLASS_IIF",_
"AST_NODECLASS_LIT",_
"AST_NODECLASS_ASM",_
"AST_NODECLASS_DATASTMT",_
"AST_NODECLASS_DBG",_
"AST_NODECLASS_BOUNDCHK",_
"AST_NODECLASS_PTRCHK",_
"AST_NODECLASS_SCOPEBEGIN",_
"AST_NODECLASS_SCOPEEND",_
"AST_NODECLASS_SCOPE_BREAK",_
"AST_NODECLASS_TYPEINI",_
"AST_NODECLASS_TYPEINI_PAD",_
"AST_NODECLASS_TYPEINI_ASSIGN",_
"AST_NODECLASS_TYPEINI_CTORCALL",_
"AST_NODECLASS_TYPEINI_CTORLIST",_
"AST_NODECLASS_TYPEINI_SCOPEINI",_
"AST_NODECLASS_TYPEINI_SCOPEEND",_
"AST_NODECLASS_PROC",_
"AST_CLASSES" }
srvaldez
Posts: 2063
Joined: Sep 25, 2005 21:54

Re: Howto initialise string array

Postby srvaldez » Aug 15, 2016 23:21

why not change from var-len string to fixed Zstring? i.e Zstring*32
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Howto initialise string array

Postby dodicat » Aug 15, 2016 23:56

If you must use a shared string array, you could fill by:

Code: Select all

sub fillstring cdecl(s() as string,t as string="",...)
    dim as string ptr p=@s(lbound(s))
    dim as zstring ptr ptr  a=cptr(zstring ptr ptr,@@t)
    var sz=ubound(s)-lbound(s)+1
    for n as long=1 to sz
        p[n-1]=**(a+N)
    next
end sub

Dim Shared As String AST_NODECLASS_STR(0 TO 44)

fillstring(AST_NODECLASS_STR(),, _
"AST_NODECLASS_NOP",_
"AST_NODECLASS_LOAD",_
"AST_NODECLASS_ASSIGN",_
"AST_NODECLASS_BOP",_
"AST_NODECLASS_UOP",_
"AST_NODECLASS_CONV",_
"AST_NODECLASS_ADDROF",_
"AST_NODECLASS_BRANCH",_
"AST_NODECLASS_JMPTB",_
"AST_NODECLASS_CALL",_
"AST_NODECLASS_CALLCTOR",_
"AST_NODECLASS_STACK",_
"AST_NODECLASS_MEM",_
"AST_NODECLASS_LOOP",_
"AST_NODECLASS_COMP",_
"AST_NODECLASS_LINK",_
"AST_NODECLASS_CONST",_
"AST_NODECLASS_VAR",_
"AST_NODECLASS_IDX",_
"AST_NODECLASS_FIELD",_
"AST_NODECLASS_DEREF",_
"AST_NODECLASS_LABEL",_
"AST_NODECLASS_ARG",_
"AST_NODECLASS_OFFSET",_
"AST_NODECLASS_DECL",_
"AST_NODECLASS_NIDXARRAY",_
"AST_NODECLASS_IIF",_
"AST_NODECLASS_LIT",_
"AST_NODECLASS_ASM",_
"AST_NODECLASS_DATASTMT",_
"AST_NODECLASS_DBG",_
"AST_NODECLASS_BOUNDCHK",_
"AST_NODECLASS_PTRCHK",_
"AST_NODECLASS_SCOPEBEGIN",_
"AST_NODECLASS_SCOPEEND",_
"AST_NODECLASS_SCOPE_BREAK",_
"AST_NODECLASS_TYPEINI",_
"AST_NODECLASS_TYPEINI_PAD",_
"AST_NODECLASS_TYPEINI_ASSIGN",_
"AST_NODECLASS_TYPEINI_CTORCALL",_
"AST_NODECLASS_TYPEINI_CTORLIST",_
"AST_NODECLASS_TYPEINI_SCOPEINI",_
"AST_NODECLASS_TYPEINI_SCOPEEND",_
"AST_NODECLASS_PROC",_
"AST_CLASSES" )

print
for n as long=0 to ubound(AST_NODECLASS_STR)
    print AST_NODECLASS_STR(n)
next n
sleep
RockTheSchock
Posts: 220
Joined: Mar 12, 2006 16:25

Re: Howto initialise string array

Postby RockTheSchock » Aug 16, 2016 0:36

Zstring*31 - that was simple!

Thank you. srvaldez

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests