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

Everything else that doesn't fall into one of the other PB categories.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

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

Post 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?
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

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

Post by #NULL »

With

Code: Select all

Pointer.String = @*Text
you would get an error: Can't assign a value to a structure.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

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

Post 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.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post by Demivec »

A structured pointer can be dereferenced but an integer holding an address has to use other methods to read and assign values.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

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

Post 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])
sorry for my bad english
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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))
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

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

Post 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.
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

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

Post by coco2 »

Thanks that makes sense. So a pointer can hold other useful information about the data.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply