Author Topic: Pointers woes  (Read 2860 times)

0 Members and 1 Guest are viewing this topic.

  • Guest
Pointers woes
« 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:

Code: [Select]
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:

Code: [Select]
dim s AS asciiz *260
s = "Test string"
dim p as asciiz ptr
@p = strptr(s)
print p

With other languages we would use

Code: [Select]
p = strptr(s)
print *p

And this other:

Code: [Select]
dim s as wide = "Test string"
wchar *p = strptr(s)
print p

And this one does not compile:

Code: [Select]
typedef double *pDouble
dim num as double = 123456.78
@pDouble = @num
print pDouble
« Last Edit: November 04, 2018, 06:29:18 PM by José Roca »

Charles Pegge

  • Guest
Re: Pointers woes
« Reply #1 on: October 03, 2018, 06:10:34 PM »
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)
Code: [Select]
typedef double *pDouble
dim as double num = 123456.78
dim as pDouble pNum
@pNum = @num 'address coupling
print pNum

José Roca

  • Guest
Re: Pointers woes
« Reply #2 on: October 03, 2018, 06:36:01 PM »
Thanks Charles. I never had used typedef.

Aurel

  • Guest
Re: Pointers woes
« Reply #3 on: October 03, 2018, 10:47:39 PM »
Most unlogical thing is this:

Code: [Select]
'pointer
 string s ="oxy"
 @p = s
print p

I simply cannot explain that to myself  ::)

address of p hold s ?

  • Guest
Re: Pointers woes
« Reply #4 on: October 12, 2018, 10:09:30 AM »
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:

Code: [Select]
#cpointer on
dim x as long = 123456
dim p as long ptr = @x
print *p
#cpointer off

instead of:

Code: [Select]
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?

José Roca

  • Guest
Re: Pointers woes
« Reply #5 on: October 12, 2018, 10:34:31 AM »
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:

Code: [Select]
#cpointer on
dim x as long = 123456
dim p as long ptr = @x
print *p
#cpointer off

instead of:

Code: [Select]
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.

JRS

  • Guest
Re: Pointers woes
« Reply #6 on: October 12, 2018, 11:11:05 AM »
Quote
It seems that anything that can be useful to me is undocumented.

Makes you wonder if you're the chicken or the egg.  :)

Aurel

  • Guest
Re: Pointers woes
« Reply #7 on: August 22, 2019, 03:43:42 PM »
Hmm
I am looking into this old topic and now i tried  to test this :

Code: [Select]
'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

jack

  • Guest
Re: Pointers woes
« Reply #8 on: August 22, 2019, 04:49:10 PM »
that code doesn't look right to me, should't print p be print *p ?

Charles Pegge

  • Guest
Re: Pointers woes
« Reply #9 on: August 22, 2019, 09:31:26 PM »
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.