Hi
how to properly cast string to pointer array and print
this code not work ( i probably for get so many things ) ::)
def a[5]:pointer
a[3]="new": a[4]="element"
MOVE win,0,60:PRINT win,#a[3]
MOVE win,0,80:PRINT win,#a[4]
Not an expert on pointers, but try:
MOVE win,0,60:PRINT win,#<STRING>a[3]
MOVE win,0,80:PRINT win,#<STRING>a[4]
Brian
Hi Brian
Something is really strange
but not work ,program just crush ::)
here is complete test program
'the lines program
DEF win:window
DEF l,t,w,h,size:int
'def a[5]:pointer
'open a window and add a menu
OPENWINDOW win,0,0,400,300,@SIZE|@MINBOX|@MAXBOX|@CAPTION,0,"Lines",&mainwindow
BEGINMENU win
MENUTITLE "Options"
MENUITEM "Clear",0,1
BEGINPOPUP "Line Size"
MENUITEM "1",0,2
MENUITEM "2",0,3
MENUITEM "3",0,4
ENDPOPUP
MENUITEM "Quit",0,5
ENDMENU
'draw the lines every 10 millesceonds
'MOVE win,20,20:PRINT win,STR$(@SIZE|@MINBOX|@MAXBOX|@CAPTION)
'MOVE win,20,40:PRINT win,STR$(@NOAUTODRAW)
def a[5]:pointer
a[3]="new": a[4]="element"
MOVE win,0,60:PRINT win,#<string>a[3]
MOVE win,0,80:PRINT win,#<string>a[4]
'
run = 1
'process messages until somebody closes us
waituntil win = 0 | run = 0
end
'this is the handler subroutine for the window
SUB mainwindow
SELECT @MESSAGE
CASE @IDCREATE
'center our window on creation
CENTERWINDOW win
CASE @IDCLOSEWINDOW
CLOSEWINDOW win
CASE @IDMENUPICK
SELECT @MENUNUM
CASE 1
RECT win,0,0,w,h,RGB(255,255,255),RGB(255,255,255)
CASE 2
CASE& 3
CASE& 4
SETLINESTYLE win,@LSSOLID,@MENUNUM - 1
size = @MENUNUM - 1
CASE 5
run = 0
ENDSELECT
CASE @IDMENUINIT
END SELECT
RETURN
END SUB
Hi Aurel,
This works ..
'the lines program
DEF win:window
DEF l,t,w,h,size:int
def a,b:string
def ptr1,ptr2:pointer
OPENWINDOW win,0,0,400,300,@SIZE|@MINBOX|@MAXBOX|@CAPTION,0,"Lines",&mainwindow
BEGINMENU win
MENUTITLE "Options"
MENUITEM "Clear",0,1
BEGINPOPUP "Line Size"
MENUITEM "1",0,2
MENUITEM "2",0,3
MENUITEM "3",0,4
ENDPOPUP
MENUITEM "Quit",0,5
ENDMENU
a = "new"
b = "element"
ptr1 = a :' point to start of string a
ptr2 = b :' point to start of string b
MOVE win,0,60:PRINT win,a + " " + #<string>ptr1
MOVE win,0,80:PRINT win,b + " " + #<string>ptr2
run = 1
'process messages until somebody closes us
waituntil win = 0 | run = 0
end
SUB mainwindow(),int
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW win :'center our window on creation
CASE @IDCLOSEWINDOW
CLOSEWINDOW win
CASE @IDMENUPICK
SELECT @MENUNUM
CASE 1
RECT win,0,0,w,h,RGB(255,255,255),RGB(255,255,255)
CASE 2
CASE& 3
CASE& 4
SETLINESTYLE win,@LSSOLID,@MENUNUM - 1
size = @MENUNUM - 1
CASE 5
run = 0
ENDSELECT
CASE @IDMENUINIT
END SELECT
RETURN(0)
END SUB
.. but I can't think it gets you very far :)
I don't think you can have an array of pointers - and they would only be pointing to the start of each string, which are laid out in memory as strings of characters anyway.
You might be better putting your descriptors in a string array. ie: desc[0] = "new","element"
and referring to the one you require with an index value 0 or 1. So desc[1] = "element".
You would then set the index you need in the program somewhere ..
Best wishes, :)
Graham
Hi Graham
and thanks for replay ...
yes i know that array of pointers is possible but i forget how casting work
so i think that i figured ..and i also interesting to see that pointer variable respond as array to
:)
'casting string pointer...........
'this shows that pointer can hold each char of string array
pointer pa[5]
pointer p1,p2
'fill pointer array elements with strings...
pa[3]="new": pa[4]="element"
'set pointer with each element....
p1=pa[3]:p2=pa[4]
MOVE win,0,60:PRINT win,#<string>p1[0]
MOVE win,0,80:PRINT win,#<string>p1[1]
MOVE win,0,100:PRINT win,#<string>p1[2]
MOVE win,0,120:PRINT win,#<string>p1
MOVE win,0,150:PRINT win,#<string>p2