[futurebasic] Re: [FB] Points on Pointers

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 2004 : Group Archive : Group : All Groups

From: Robert Covington <artlythere@...>
Date: Wed, 22 Sep 2004 22:45:22 -0400
On Wednesday, September 22, 2004, at 10:13  PM, Ken Shmidheiser wrote:

> I am working to increase my understanding of pointers. In this code I 
> am trying to use a pointer to obtain the value of any element in an 
> array. I think what I'm seeing here is the element's address in 
> memory, but I don't understand how to dereference the pointer to 
> return the value of the array element itself.
>
> My confession for tonight: I've been ignorant of pointers too long and 
> am aspiring to the heights of Jay. While I understand the car 
> keys-parking lot illustration of pointing to memory, what I need is 
> some practical help in using pointers in FB4 with some simple examples 
> in code.
>
> Believe it or not, I have had more success in understanding pointers 
> in C, because the simple * syntax is used consistently for 
> dereferencing unlike FB.
>
> Any pointers?
>
> (Sorry, couldn't resist.)
>
> Ken

You can just use a pointer var, don't need a pointer to array pointer, 
whatevers.
This returns an unsigned int however using the assembly quickie...but 
Peek Word works nice enough.

Note: Window is 800 wide to show all 4 across...
r
c


_arrayElements = 6

dim as int myArr( _arrayElements ),var

myArr(0) = _arrayElements
myArr(1) =   1
myArr(2) =  23
myArr(3) =  17
myArr(4) =   4
myArr(5) =  -5
myArr(6) = 100

local fn PickinNPointin
dim as int  i
dim as ptr  myPtr

myPtr = @myArr(0)

// myPtr points to memory at element 0
for i = 1 to myArr(0)
myPtr += 2 // now points at index 1, then the next...
print "Via loop: myArr("; i; " ) ="; myArr(i),
print "Via pointer: arrayPrt + 1 ="; myPtr.0%`, // unsigned int will be 
displayed...
var = myPtr.0%` // easier way to do this no doubt.
print "Signed from pointer...";var,
print "Via Peek word:";Peek Word (myPtr)
next i

end fn

window 1,"Results",(0,0)-(800,120),_docNoGrow
fn PickinNPointin

include "Subs Quick Event Loop.Incl"