Oxygen Basic
Programming => Problems & Solutions => Topic started by: on October 03, 2018, 03:40:57 PM
-
Working with pointers with O2 is most confussing. Almost nothing seems to work like with other languages. A detailed explanation would be needed.
This one works as I would expect:
int a = 42
sys b ' sys ensures an integer large enough to hold a pointer
b = @a ' assign address of a to b
print *b ' 42
But this one works differently:
dim s AS asciiz *260
s = "Test string"
dim p as asciiz ptr
@p = strptr(s)
print p
With other languages we would use
p = strptr(s)
print *p
And this other:
dim s as wide = "Test string"
wchar *p = strptr(s)
print p
And this one does not compile:
typedef double *pDouble
dim num as double = 123456.78
@pDouble = @num
print pDouble
-
Yes, it can be confusing. o2 uses implicit pointering, which means the dereferencing of variables in expressions is automatic. And you only need the occasional '@' operator to reposition a variable.
This works: (you were trying to use type pDouble as a variable)
typedef double *pDouble
dim as double num = 123456.78
dim as pDouble pNum
@pNum = @num 'address coupling
print pNum
-
Thanks Charles. I never had used typedef.
-
Most unlogical thing is this:
'pointer
string s ="oxy"
@p = s
print p
I simply cannot explain that to myself ::)
address of p hold s ?
-
I have found another undocumented directive, #cpointer. It seems that anything that can be useful to me is undocumented :)
Using it, pointers behave like with other languages I'm used:
#cpointer on
dim x as long = 123456
dim p as long ptr = @x
print *p
#cpointer off
instead of:
dim x as long = 123456
dim p as long ptr
@p = @x
print p
It is an already established directive or it is still experimental?
-
Hope I have got it right:
#cpointer
Specifies to use C-like syntax with pointers.
Example
Wrapping code between #cpointer on and #cpointer off directives allows to use explicit pointers that are deferenced with the * operator:
#cpointer on
dim x as long = 123456
dim p as long ptr = @x
print *p
#cpointer off
instead of:
dim x as long = 123456
dim p as long ptr
@p = @x
print p
By default, Oxygen Basic uses a novelty syntax style to avoid the use of explicit pointers. This applies uniformly to objects, functions and variables. Also to COM objects. Once an indirect variable is given an address, no further pointer notation is required.
The principle is that once an indirect variable is provided with an address, it is treated just like a normal variable. This is exactly how byref parameters work within functions for most basics.
-
It seems that anything that can be useful to me is undocumented.
Makes you wonder if you're the chicken or the egg. :)
-
Hmm
I am looking into this old topic and now i tried to test this :
'pointer
string s = "Aurel"
int p As string ptr
p = @s
print p
wow ...and work , i see this when Jose tested asciiz so i tried with string and work .
But just in a given shape.
I must try that with SELECT... :D
-
that code doesn't look right to me, should't print p be print *p ?
-
It works because o2 automatically resolves the indirection to locate the string content.
strings have 2 levels, and in this example, p has 3 levels.