Page 1 of 1

What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 10:17 am
by coco2
I've just noticed that this code works:

Code: Select all

Text$ = "Hello"
*Text = @Text$
Text.i = @Text$
Debug *Text
Debug Text
*Pointer.String = @*Text
*Pointer2.String = @Text
Debug *Pointer\s
Debug *Pointer2\s
What is the benefit of using a pointer with the asterisk?

Re: What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 11:06 am
by #NULL
With

Code: Select all

Pointer.String = @*Text
you would get an error: Can't assign a value to a structure.

Re: What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 12:32 pm
by spikey
In fact a variable is a pointer, when you get down to the microprocessor level.

When you use variables in source code the compiler sometimes automatically performs extra actions that might get in the way, for example when passing strings in and out of external library procedures. The * notation tells the compiler not to do anything special that might cause interference.

Re: What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 5:43 pm
by Demivec
A structured pointer can be dereferenced but an integer holding an address has to use other methods to read and assign values.

Re: What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 7:56 pm
by mk-soft
A string variable is always a pointer direct to a string.

But here comes what you have to pay attention to.
Because PB manages the string variables itself, the pointer always changes on the string.

But if you want to edit strings ByRef you have to work with pointer on the string variable.

A string variable as pointer is always created with "Var.String".

Code: Select all

Procedure Concat(*Result.String, s1.s, s2.s)
  *Result\s = s1 + s2 + Space(50)
  ProcedureReturn Len(*Result\s)
EndProcedure

Define r1.String
r1\s = "ByRef"

Debug "Adr to String = " + PeekI(r1)
Len = Concat(r1, "Hello ", "World")
Debug "Adr to String = " + PeekI(r1)
Debug r1\s
Debug PeekS(PeekI(r1))

End

; *this crashed because @r2 is direct a address to a string
Define r2.s
r2 = "ByVal"

Debug "Adr to String = " + @r2
Len = Concat(@r2, "Hello ", "World")
Debug "Adr to String = " + @r2
Debug r2
Debug PeekS(@r2)

Re: What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 9:49 pm
by Josh
mk-soft wrote:A string variable is always a pointer direct to a string.
Not quite. Only string variables with a fixed length are a pointer direct to the string. String variables without a fixed length are pointers to a pointer showing to the string.

Code: Select all

Structure CHARARRAY
  c.c[0]
EndStructure

Structure MYSTRUC
  Dummy.i
  String2.s
  String3.s{20}
EndStructure

Define MyStruc.MYSTRUC
Define *StrucField1
Define *StrucField2.CHARARRAY
Define *StrucField3.CHARARRAY

*StrucField1 = @MyStruc\Dummy
*StrucField2 = *StrucField1 + SizeOf (Integer)
*StrucField3 = *StrucField2 + SizeOf (Integer)

MyStruc\String2 = "222"
MyStruc\String3 = "333"

;Standard String
*StrucField2 = PeekI (*StrucField2)
Debug "" + Chr (*StrucField2\c[0]) + Chr (*StrucField2\c[1]) + Chr (*StrucField2\c[2])

;Fixlen String
Debug "" + Chr (*StrucField3\c[0]) + Chr (*StrucField3\c[1]) + Chr (*StrucField3\c[2])

Re: What does a pointer do that an integer doesn't?

Posted: Sat Mar 16, 2019 11:54 pm
by mk-soft
Thats is right.

This structure is a same as Var.String

Code: Select all

Structure MyStringPointer
  s.s
EndStructure

Procedure Concat(*Result.MyStringPointer, s1.s, s2.s)
  *Result\s = s1 + s2 + Space(50)
  ProcedureReturn Len(*Result\s)
EndProcedure

Define r1.MyStringPointer
r1\s = "ByRef"

Debug "Adr to String = " + PeekI(r1)
Len = Concat(r1, "Hello ", "World")
Debug "Adr to String = " + PeekI(r1)
Debug r1\s
Debug PeekS(PeekI(r1))

Re: What does a pointer do that an integer doesn't?

Posted: Tue Apr 02, 2019 1:41 pm
by Josh
Demivec wrote:A structured pointer can be dereferenced ...
The fact that a pointer can be dereferenced has nothing to do with the fact that it has a structure.

Re: What does a pointer do that an integer doesn't?

Posted: Tue Apr 02, 2019 2:41 pm
by Demivec
Josh wrote:
Demivec wrote:A structured pointer can be dereferenced ...
The fact that a pointer can be dereferenced has nothing to do with the fact that it has a structure.
Example:

Code: Select all

Define myInteger = 1234567
Define myIntegerAddr = @myInteger 
Define *ptrUnstructured = @myInteger
Define *ptrStructured.Integer = @myInteger 

Debug myInteger ;displays 1234567
Debug *ptrStructured\i ;dereferencing is done as an aspect of the pointer's definition
Debug PeekI(myIntegerAddr) ;dereferencing has to be done by using other methods
Debug PeekI(*ptrUnstructured) ;dereferencing is just like any other integer holding an address

Re: What does a pointer do that an integer doesn't?

Posted: Wed Apr 03, 2019 2:24 am
by coco2
Thanks that makes sense. So a pointer can hold other useful information about the data.

Re: What does a pointer do that an integer doesn't?

Posted: Wed Apr 03, 2019 3:34 am
by skywalk
A benefit to my feeble brain, is PB pointers retain their * prefix throughout my code.
In C, that is lost unless you name the variable ptr_something or p_something.
And then poof, p as a variable prefix is taken.
With modern IDE's and intellisense, you get colorized and hover notice of datatypes and pointers.
But, I still prefer the direct *ptr telling me this is a pointer.

Even then, PB has quirks with this when getting structure elements that are *pointers.

Code: Select all

Define myInteger = 1234567
Define myIntegerAddr = @myInteger
Define *ptrUnstructured = @myInteger
Define *ptrStructured.Integer = @myInteger
Debug myInteger               ;displays 1234567
Debug *ptrStructured\i        ;dereferencing is done as an aspect of the pointer's definition
Debug PeekI(myIntegerAddr)    ;dereferencing has to be done by using other methods
Debug PeekI(*ptrUnstructured) ;dereferencing is just like any other integer holding an address
Structure myS
  a.i
  *p_i.Integer  ;<-- * is dropped when dereferencing later within a structure.
  pp.d
EndStructure
Define *my1.myS = AllocateMemory(SizeOf(myS))
*my1\p_i = @myInteger
Debug *my1\p_i\i
Define my1.myS
my1\p_i = @myInteger
Debug my1\p_i\i