Accessing value from Pointer

BlitzMax Forums/BlitzMax Programming/Accessing value from Pointer

BLaBZ(Posted 2010) [#1]
So this is how you access a value from a pointer..
Local num:Int = 4
Local numPtr:Int Ptr = Varptr(num)
Print Int(numPtr)

Print numPtr[0]

Though it seems odd to me, why an array to access its value?

I feel like convention would suggest some sort of FromPtr function?!

What value am I receiving when the array index is increased?
ex:
Print numPtr[1]
Print numPtr[2]



Jesse(Posted 2010) [#2]
from the example above you are accessing an out of bound address and can possibly be causing a MAV(memory access violation) and may potentially crash your program so unless you know that the next address is, and is something you have stored in there, don't try to access it or write to it.


Jesse(Posted 2010) [#3]

Though it seems odd to me, why an array to access its value?


all of the memory addresses are in a sort of array format(its a long strip of bytes). what varptr does, is get the address where the the value for the variable is located. the index allows you to see everything following the current address one integer at a time('int Ptr' set it up that way.)


BLaBZ(Posted 2010) [#4]
Ok I get it,
So I'm safe to use numPtr[0] correct? When I use numPtr[1] am I sort of shifting the memory address in which its reading from?


Jesse(Posted 2010) [#5]
correct.


Czar Flavius(Posted 2010) [#6]
This is carried over from C land where arrays are just a pointer to a strip of memory. If you know your int pointer is pointing to the begining of an int array, for example, you can use [x] notation to access the elements of that array. You could also point a byte pointer to an integer value, and use [0-3] to split the integer up into 4 parts.