[futurebasic] Re: [FB] Pointers

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2005 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Mon, 25 Apr 2005 19:56:03 +0200
Joe Smith a écrit :
> I'm playing around with pointers and am having a problem reading back the
> contents of the pointer after loading it with a number. The statement in the
> following code that's giving me the problem is: print
> k,chr$(peek(MyPtr&+K)).
> What am I doing wrong ??
> 
> Also, is there anyway of getting the number of characters in the number
> without resorting to copying it into a string and getting the length of the
> string ?? ... Kind of a sizeof thing ??
> 
> J Smith
> 
> 
> dim @myNumber&
> dim @myNumberAgain&
> Dim MyPtr
> 
> 
> myNumber&   = 1234567890
Above, you are setting a long integer variable that uses 4 bytes of 
memory only.

> MyPtr&      = fn newptrclear (12)
You only need a pointer of the same size (i.e. 4 bytes)

> blockmove  @myNumber&, myPtr&, 10
> 
Again, you must move only four bytes.

> ptrSize& = fn getPtrSize(myPtr&)
> 
> for K=0 to ptrSize&
> print k,chr$(peek (MyPtr&+K))
You are attempting to read the ASCII code starting with the internal 
representation of a long int, it doesn't make that much sense, although 
it might be relevant had you set the long int variable like this maybe:

myNumber& = _"Test"

> next K
> '----------------------------
> ' Retrieve data from pointer
> '----------------------------
> blockmove myPtr&, @myNumberAgain&, 10
> print "myNumberAgain&="myNumberAgain& ' This works fine.
> 

Below a complete example:

dim @myNumber&
dim @myNumberAgain&
dim MyPtr&,ptrSize&,k&


myNumber&   = 1415934836
MyPtr&      = fn NewPtrClear(sizeof(long))
blockmove  @myNumber&, myPtr&, sizeof(long)

ptrSize& = fn GetPtrSize(myPtr&)

for K&=0 to ptrSize& -1
print k&,chr$(peek (MyPtr&+K&))
next K&
'----------------------------
' Retrieve data from pointer
'----------------------------
blockmove myPtr&, @myNumberAgain&, 4
print "myNumberAgain&="myNumberAgain& ' This works fine.

Alain