Question about Int and Float Pointers

BlitzMax Forums/BlitzMax Beginners Area/Question about Int and Float Pointers

Chroma(Posted 2010) [#1]
Is there a way to make a pointer than will accept both an Int and a Float?


xlsior(Posted 2010) [#2]
Given that they are stored differently in memory and the whole point of a pointer is that it points to a specific memory location, I seriously doubt you'll be able to make it work without converting either to one or the other before passing the data...


Czar Flavius(Posted 2010) [#3]
Why do you want to do this? You could make a type which contains an int and a float. Edit: and just use one of them at a time.

Last edited 2010


Floyd(Posted 2010) [#4]
The answer is probably no, at least not in the form you want.

A Byte Ptr can hold an address of any type of number. But it doesn't "remember" what that type was. I just knows the address. So you have keep track of the data type yourself and cast the pointer to the appropriate type.

Local p:Byte Ptr

n:Int = 7
x# = 123.123123

p = Varptr(n)
nn:Int = (Int Ptr p)[0]

p = Varptr(x)
xx# = ( Float Ptr p )[0]

Print
Print nn
Print xx



ImaginaryHuman(Posted 2010) [#5]
Yah yo'd have to cast it in order to `interpret it`. It just means you have to manually hardcode your interpretations, like in the example above. You can't have a single pointer that will sometimes pass back a float and sometimes an int. But if you know that what the type is of a given piece of data (using an associative array?) then you can explicitly read it as a float or an int. You just have to case the type of the array to the type you want, ie if you set up an array of int, you'd need to cast it to a float array before reading it. This does work. I made an array of function pointers and sometimes stored a function pointer, sometimes integers. You just have to know which is which and when you read using the appropriate casting.

Last edited 2010