June 15, 2024, 07:13:19 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


pointer question

Started by talun, April 24, 2009, 03:41:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

talun

Hi,
according to the help,
QuoteA simpler method to access a pointer to an array of data is to use the standard array indexes. In this manner you do not have to account for the size of the variable, as the type cast will specify the size of the data being stored.
I'm trying to play with a dynamic string array with wrong results


string temp
int totmsg,nmsg

totmsg = 10

pointer pDimMsg = new(string,totmsg)

for nmsg = 1 to totmsg
   temp = string$(5,chr$(64+nmsg))
   #<string>pDimMsg[nmsg-1] = temp
   print temp," ---> ",#<string>pDimMsg[nmsg-1]
next nmsg

delete pDimMsg

do:until inkey$ <> ""
end


Can someone say me where I'm wrong?

Thanks!

Sergio

LarryMc

Probably not the best way but this is one way to get the correct results.
string temp
int totmsg,nmsg

totmsg = 10
pointer porig
pointer pDimMsg = new(string,totmsg)
porig=pDimMsg

for nmsg = 1 to totmsg-1
   temp = string$(5,chr$(64+nmsg))
   #<string>pDimMsg = temp
   print temp," ---> ",#<string>pDimMsg
pDimMsg+=255
next nmsg

delete porig

do:until inkey$ <> ""
end


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

April 24, 2009, 10:36:54 AM #2 Last Edit: April 24, 2009, 04:27:55 PM by Paul Turley
A string array has two dimensions. The implicit one is 255 characters, so you have to specify both indexes.  In this case you want the first character location (0) of each string (0-9)


string temp
int totmsg,nmsg

totmsg = 10

pointer pDimMsg = new(string,totmsg)

for nmsg = 0 to totmsg-1
   temp = string$(5,chr$(64+nmsg+1))
   #<string>pDimMsg[0,nmsg] = temp
   print temp," ---> ",#<string>pDimMsg[0,nmsg]
next nmsg

delete pDimMsg

do:until inkey$ <> ""
end


Think of it this way.  You know a string is 255 CHARacters.  So if you wanted to allocate the same memory using a CHAR it would look like this:

pointer pDimMsg = new(CHAR,255 * totmsg)

Which is a dimension of [255,10]

Paul.
Ionic Wind Support Team

talun